I created one stored procedure; it executed successfully but while executing manually it is showing error.
Here’s my stored proc
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
After execution of this stored procedure, command is successfully and now I am inserting:
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'
I am getting the error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘>’.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ‘stream’
Msg 105, Level 15, State 1, Line 11
Unclosed quotation mark after the character string ‘) end ‘.
I don’t know how to clear this error, I didn’t find any error near ‘>’ this symbol, can you guys help me out to clear this error?
Besides the design where you have a single table per user, your issue is that the first line of SQL doesn’t have a space before the IF statement. If you want line breaks in the SQL, then you need to add
+ CHAR(10)and not have the line breaks directly in the code.If you are keeping the dynamic SQL, I would suggest reformatting it the set statement as follows:
However, I would strongly encourage you, if possible, to just have a single BandWidth table that contains columns that hold the UserName and UserId. Then you won’t have to have dynamic SQL at all.