SP中执行动态组串的sql

我写过的一个SP

CREATE PROCEDURE epcPasswordIsLastNUsed
 @epcUserID   int,
 @NewPassword VARCHAR(50),
 @LastN   Varchar(5)
AS
 declare @sql nvarchar(200)
 declare @IsUsedLastN int
  
 set @sql = 'If ''' + @NewPassword + ''' in
 (Select top ' + @LastN + ' [Password]  from epcPassword
 Where epcUserID = '+ CAST ( @epcUserID AS varchar(10) )  +' Order By epcPasswordID Desc)
 Set @IsUsed =1 else set @IsUsed = 0'
 
 exec sp_executesql       @sql,N'@IsUsed int output',@IsUsedLastN output      
 return @IsUsedLastN
 
 GO
GRANT  EXECUTE  ON [dbo].[epcPasswordIsLastNUsed]  TO [public]
GO

posted on 2008-09-08 15:42  皮皮虾的blog  阅读(320)  评论(0编辑  收藏  举报