且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sql server 2008中使用两个参数在商店过程中使用top子句。

更新时间:2023-02-22 18:38:56





您在比较字符串值时错过了单引号('')



请尝试以下代码



Hi,

You missed the Single Quote ('') while compare String values

Try the following code

CREATE PROC wiz_msg1 @usrnm VARCHAR(500),@sndnm VARCHAR(500),@ctr INT
AS BEGIN
EXEC('SELECT top '+CAST(@ctr AS VARCHAR(10))+'
Serial_No,
Sent_From,
Message,
UserName,
Date
FROM wizom_Receive_Knocks
WHERE Sent_From in('+CHAR(39)+@usrnm+CHAR(39)+','+CHAR(39)+@sndnm+CHAR(39)+')and UserName in('+CHAR(39)+@usrnm+CHAR(39)+','+CHAR(39)+@sndnm+CHAR(39)+') order by date desc')
END




-- Execute Statement 
exec wiz_msg1 'balbinder123','sachin',10 





问候,

GVPrabu



Regards,
GVPrabu


你收到了错误。 b''cos你错过了''+ @ usrnm +''附近的单引号,''+ @ sndnm''



你必须加三个单引号才能显示动态sql中的单引号。



我已修改你的qury如下:



You got error. b''cos you have missed single quotation near ''+@usrnm+'',''+@sndnm''

you have to add three single quot to display single quot in dynamic sql.

I have modified your qury like below:

declare @ctr int
set @ctr = 1
declare @usrnm varchar(500),@sndnm varchar(500)
set @usrnm = 'ww'
set @sndnm = 'qq'

declare @sql nvarchar(max)
set @sql ='SELECT top '+ cast(@ctr as varchar(max)) +' Serial_No, Sent_From, Message, UserName, [Date] FROM wizom_Receive_Knocks WHERE Sent_From in('''+@usrnm+''','''+@sndnm+''')and UserName in('''+@usrnm+''','''+@sndnm+''') order by date desc'
select @sql

exec (@sql)