2011-11-11 3 views
0

ストアドプロシージャを1つ作成しました。正常に実行されましたが、手動で実行中にエラーが表示されます。エラーを表示せずにストアドプロシージャを作成しましたが、エラーが発生して手動で実行しました

は、ここでは、このストアドプロシージャの実行後、私のストアドプロシージャ

USE [chandru] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
alter procedure [dbo].[Insert_BandWidthDetails]   
@CurrentState nvarchar(50),@Process nvarchar(100),@DateTime nvarchar(100),@IPaddress nvarchar(50),@UploadedBytes nvarchar(max),@DownloadedBytes nvarchar(max),@Duration nvarchar(200),@FileSize nvarchar(max),@StreamId nvarchar(100),@PlayerId nvarchar(100), 
@UserName nvarchar(200),@UserId nvarchar(200),@CountryName nvarchar(100),@RegionName nvarchar(100),@Latitude nvarchar(100),@Longitude nvarchar(100),@City nvarchar(100)   
as   
begin  


declare @Sql nvarchar(max)  

set @Sql='declare @countbandwidthtable int select @countbandwidthtable=COUNT(*) from BandWidth'[email protected][email protected]+ 
    +'if(@countbandwidthtable>0) 
    begin 
     declare @count int select @count=COUNT(*) from BandWidth'[email protected][email protected]+' where CurrentState='''[email protected]+''' and Process='''[email protected]+''' and DateTime='''[email protected]+''' and IPaddress='''[email protected]+''' and UploadedBytes='''[email protected]+''' and DownloadedBytes='''[email protected]+''' and Duration='''[email protected]+''' and FileSize='''[email protected]+''' and StreamId='''[email protected]+''' and PlayerId='''[email protected]+''' and UserName='''[email protected]+'' 
     +'if(@count=0)   
     begin   
      insert into BandWidth'[email protected][email protected]+' values('''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''')  
     end   
    end 
else 
    begin 
     select * into BandWidth'+ @UserName+ cast(@UserID as nvarchar(max)) +' from BandWidthSample where 1=2 
     insert into BandWidth'[email protected][email protected]+' values('''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''')  
    end '  
exec(@Sql) 
end 

だ、コマンドが正常であると今私が挿入しています:

Insert_BandWidthDetails 'stream','play','11:17:00','10.0.3.0','12344','1234','2.09','22','1','11223','sample','31','india','asd','23','23','chennai' 

が、私はエラー

メッセージを取得しています102、レベル15、状態1、行1
'>'付近の構文が正しくありません。
メッセージ102、レベル15、状態1、行 'ストリーム'
メッセージ105、レベル15、状態1に近い5
不適切な構文、11
閉じられていない引用符文字列 ')' 末端の後の行。

このエラーを解決する方法はわかりませんが、このシンボル付近でエラーが見つかりませんでした。このエラーをクリアするには手伝ってもらえますか?

+0

exec(@sql)文をコメントアウトし、print @Sql文を追加してください。メッセージウィンドウに文が印刷されます。ステートメントをコピーし、新しいクエリウィンドウでそれを実行しようとします。特に、intellisenseを使用する場合は、構文エラーを簡単に確認できるはずです。 – brian

+0

ありがとうございました – user1005078

答えて

1

ユーザーごとに1つのテーブルがあるという設計に加えて、SQLの最初の行にIF文の前にスペースがないことが問題です。 SQLで改行が必要な場合は、+ CHAR(10)を追加し、コードに改行を直接入れる必要はありません。

私は次のようにそれを設定した文を再フォーマットすることをお勧めは、動的SQLを維持している場合:

set @Sql='declare @countbandwidthtable int' + CHAR(10) + 
     'select @countbandwidthtable=COUNT(*) from BandWidth' + @UserName + @UserId + CHAR(10) + 
     'if(@countbandwidthtable>0)' + CHAR(10) + 
      'begin' + CHAR(10) + 
      'declare @count int' + CHAR(10) + 
      'select @count=COUNT(*) from BandWidth'[email protected][email protected]+' where CurrentState='''[email protected]+''' and Process='''[email protected]+''' and DateTime='''[email protected]+''' and IPaddress='''[email protected]+''' and UploadedBytes='''[email protected]+''' and DownloadedBytes='''[email protected]+''' and Duration='''[email protected]+''' and FileSize='''[email protected]+''' and StreamId='''[email protected]+''' and PlayerId='''[email protected]+''' and UserName='''[email protected]+'' + CHAR(10) + 
      'if(@count=0)' + CHAR(10) + 
       'begin' + CHAR(10) + 
       'insert into BandWidth'[email protected][email protected]+' values('''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''')' + CHAR(10) + 
       'end' + CHAR(10) + 
      'end' + CHAR(10) + 
     'else' + CHAR(10) + 
      'begin' + CHAR(10) + 
      'select * into BandWidth'+ @UserName+ cast(@UserID as nvarchar(max)) +' from BandWidthSample where 1=2' + CHAR(10) + 
      'insert into BandWidth'[email protected][email protected]+' values('''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''')' + CHAR(10) + 
      'end' + CHAR(10) 

可能な場合しかし、私は強くちょうど含む単一帯域テーブルを持っている、ことをお勧めしますUserNameとUserIdを保持する列。そうすれば、動的SQLをまったく持っている必要はありません。最後に「2新を見て、ここで

declare @count int select @count=COUNT(*) from BandWidth'[email protected][email protected]+' where CurrentState='''[email protected]+''' and Process='''[email protected]+''' and DateTime='''[email protected]+''' and IPaddress='''[email protected]+''' and UploadedBytes='''[email protected]+''' and DownloadedBytes='''[email protected]+''' and Duration='''[email protected]+''' and FileSize='''[email protected]+''' and StreamId='''[email protected]+''' and PlayerId='''[email protected]+''' and UserName='''[email protected]+'' 

declare @count int select @count=COUNT(*) from BandWidth'[email protected][email protected]+' where CurrentState='''[email protected]+''' and Process='''[email protected]+''' and DateTime='''[email protected]+''' and IPaddress='''[email protected]+''' and UploadedBytes='''[email protected]+''' and DownloadedBytes='''[email protected]+''' and Duration='''[email protected]+''' and FileSize='''[email protected]+''' and StreamId='''[email protected]+''' and PlayerId='''[email protected]+''' and UserName='''[email protected]+'''' 

に@Username後:

+0

ありがとうございました – user1005078

+0

それは良いニュースです。答えがあなたのために問題を解決したので、答えの隣にあるチェックボックスをチェックして、将来の訪問者があなたの問題を解決した答えを知ることができますか? –

+0

確かに、私はそれをやった。もう一度ありがとう – user1005078

0

てみてください、その変更を行います。それは唯一の変化です。 それが働いたことを教えてください

関連する問題