2011-11-11 33 views
0

我创建了一个存储过程;它执行成功,但手动执行时显示错误。我创建了一个存储过程,但执行没有错误,但手动运行它显示错误

这里是我的存储过程

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,Level 15,State 1,Line 1
'>'附近语法不正确。
消息102,级别15,状态1,行邻近 '流'
消息105,级别15,状态1 5
的语法不正确,行字符串 ')' 末端后11
闭合的引号。

我不知道如何清除这个错误,我在'>'这个符号附近没有发现任何错误,你们可以帮我解决这个错误吗?

+0

注释掉exec(@sql)语句并添加一个print @Sql语句。您的声明将打印在消息窗口中。复制语句并尝试在新的查询窗口中运行该语句。您应该能够轻松地看到语法错误,特别是在使用智能感知的情况下。 – brian

+0

感谢其工作 – user1005078

回答

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'[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。

+0

感谢它的工作 – user1005078

+0

这是个好消息。由于答案可以解决您的问题,您是否可以选中答案旁边的复选框,以便未来的访问者知道答案为您解决了问题? –

+0

你确定,我做到了。再次感谢 – user1005078

0

尝试进行改变:

这里

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]e+'''' 

@username后,查看了2个新的'到底。这是唯一的变化。 告诉我它的工作原理

相关问题