我创建了一个存储过程;它成功执行但在手动执行时显示错误。
这是我的存储过程
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'+@UserName+@UserId+
+'if(@countbandwidthtable>0)
begin
declare @count int select @count=COUNT(*) from BandWidth'+@UserName+@UserId+' where CurrentState='''+@CurrentState+''' and Process='''+@Process+''' and DateTime='''+@DateTime+''' and IPaddress='''+@IPaddress+''' and UploadedBytes='''+@UploadedBytes+''' and DownloadedBytes='''+@DownloadedBytes+''' and Duration='''+@Duration+''' and FileSize='''+@FileSize+''' and StreamId='''+@StreamId+''' and PlayerId='''+@PlayerId+''' and UserName='''+@UserName+''
+'if(@count=0)
begin
insert into BandWidth'+@UserName+@UserId+' values('''+@CurrentState+''','''+@Process+''','''+@DateTime+''','''+@IPaddress+''','''+@UploadedBytes+''','''+@DownloadedBytes+''','''+@Duration+''','''+@FileSize+''','''+@StreamId+''','''+@PlayerId+''','''+@UserName+''','''+@CountryName+''','''+@RegionName+''','''+@Latitude+''','''+@Longitude+''','''+@City+''')
end
end
else
begin
select * into BandWidth'+ @UserName+ cast(@UserID as nvarchar(max)) +' from BandWidthSample where 1=2
insert into BandWidth'+@UserName+@UserId+' values('''+@CurrentState+''','''+@Process+''','''+@DateTime+''','''+@IPaddress+''','''+@UploadedBytes+''','''+@DownloadedBytes+''','''+@Duration+''','''+@FileSize+''','''+@StreamId+''','''+@PlayerId+''','''+@UserName+''','''+@CountryName+''','''+@RegionName+''','''+@Latitude+''','''+@Longitude+''','''+@City+''')
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'
我收到错误
Msg 102,Level 15,State 1,Line 1
'>'附近的语法不正确。
Msg 102,Level 15,State 1,Line 5
'stream'附近的语法不正确
Msg 105,Level 15,State 1,Line 11
字符串')结束后的未闭合引号。
我不知道如何清除此错误,我在“>”附近未发现任何错误这个符号,你们可以帮我解决这个错误吗?
答案 0 :(得分:1)
除了每个用户只有一个表的设计之外,您的问题是第一行SQL在IF语句之前没有空格。如果您想在SQL中使用换行符,则需要添加+ CHAR(10)
并且不要在代码中直接使用换行符。
如果要保留动态SQL,我建议将set语句重新格式化如下:
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'+@UserName+@UserId+' where CurrentState='''+@CurrentState+''' and Process='''+@Process+''' and DateTime='''+@DateTime+''' and IPaddress='''+@IPaddress+''' and UploadedBytes='''+@UploadedBytes+''' and DownloadedBytes='''+@DownloadedBytes+''' and Duration='''+@Duration+''' and FileSize='''+@FileSize+''' and StreamId='''+@StreamId+''' and PlayerId='''+@PlayerId+''' and UserName='''+@UserName+'' + CHAR(10) +
'if(@count=0)' + CHAR(10) +
'begin' + CHAR(10) +
'insert into BandWidth'+@UserName+@UserId+' values('''+@CurrentState+''','''+@Process+''','''+@DateTime+''','''+@IPaddress+''','''+@UploadedBytes+''','''+@DownloadedBytes+''','''+@Duration+''','''+@FileSize+''','''+@StreamId+''','''+@PlayerId+''','''+@UserName+''','''+@CountryName+''','''+@RegionName+''','''+@Latitude+''','''+@Longitude+''','''+@City+''')' + 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'+@UserName+@UserId+' values('''+@CurrentState+''','''+@Process+''','''+@DateTime+''','''+@IPaddress+''','''+@UploadedBytes+''','''+@DownloadedBytes+''','''+@Duration+''','''+@FileSize+''','''+@StreamId+''','''+@PlayerId+''','''+@UserName+''','''+@CountryName+''','''+@RegionName+''','''+@Latitude+''','''+@Longitude+''','''+@City+''')' + CHAR(10) +
'end' + CHAR(10)
但是,如果可能的话,我强烈建议您只使用一个包含保存UserName和UserId的列的BandWidth表。那么你根本不需要动态SQL。
答案 1 :(得分:0)
尝试进行更改:
下面
declare @count int select @count=COUNT(*) from BandWidth'+@UserName+@UserId+' where CurrentState='''+@CurrentState+''' and Process='''+@Process+''' and DateTime='''+@DateTime+''' and IPaddress='''+@IPaddress+''' and UploadedBytes='''+@UploadedBytes+''' and DownloadedBytes='''+@DownloadedBytes+''' and Duration='''+@Duration+''' and FileSize='''+@FileSize+''' and StreamId='''+@StreamId+''' and PlayerId='''+@PlayerId+''' and UserName='''+@UserName+''
到
declare @count int select @count=COUNT(*) from BandWidth'+@UserName+@UserId+' where CurrentState='''+@CurrentState+''' and Process='''+@Process+''' and DateTime='''+@DateTime+''' and IPaddress='''+@IPaddress+''' and UploadedBytes='''+@UploadedBytes+''' and DownloadedBytes='''+@DownloadedBytes+''' and Duration='''+@Duration+''' and FileSize='''+@FileSize+''' and StreamId='''+@StreamId+''' and PlayerId='''+@PlayerId+''' and UserName='''+@UserName+''''
在@userName之后,最后查看2个新的'。这是唯一的变化。 告诉我它是否有效