在編寫sql中,經常需要對sql進行預處理,動態拼接字串,那麼要獲取在這預處理之後返回的的某個值並且賦值,傳入引數的語法
USE [OA]
GO
/****** Object: StoredProcedure [dbo].[usp_oa_UserMessageProve_Update] Script Date: 12/02/2014 10:45:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
功能:更新使用者證明資料,更新使用者證明狀態
2014.10.28 16:14 am
*/
ALTER PROC [dbo].[usp_oa_UserMessageProve_Update]
@msgid VARCHAR(10)
,@userid VARCHAR(10)
,@state VARCHAR(10)
,@remark VARCHAR(200)
,@filepath VARCHAR(200)
AS
begin
DECLARE
@tablename VARCHAR(100)
,@provered INT
,@sqlstr NVARCHAR(1000)
,@provepath VARCHAR(1000)
,@NOT_PROVE INT --未證明
,@CANNOT_PROVE INT --無法完成
,@HAS_PROVE INT --已證明
--無法證明更新
SELECT @NOT_PROVE=56,@CANNOT_PROVE=58,@HAS_PROVE=57
select @tablename=tablename from ADM_F_GETUSERREADTABLE (@userid)
IF @state = '1' -- state =1 執行無法證明操作
BEGIN
SET @sqlstr = 'update '+@tablename+' set proveremark='''+@remark+''',provered='+CAST(@CANNOT_PROVE AS VARCHAR(10))
+ ' where UserID='+@userid + ' and MessageID='+@msgid
EXEC(@sqlstr)
END
ELSE IF @state = '2' -- state = 2 執行 按鈕證明操作
BEGIN
SET @sqlstr = 'update '+@tablename+' set provered='+CAST(@HAS_PROVE AS VARCHAR(10))
+ ' where UserID='+@userid + ' and MessageID='+@msgid
EXEC(@sqlstr)
END
ELSE IF @state = '3' -- state = 3 執行 檔案路徑更新操作
BEGIN
SET @sqlstr = 'update '+@tablename+' set provered='+CAST(@HAS_PROVE AS VARCHAR(10))+',provepath = isnull(provepath,'''')+ '''+@filepath
+','' where UserID='+@userid + ' and MessageID='+@msgid
EXEC(@sqlstr)
END
ELSE IF @state = '4' -- state = 4 執行檔案刪除更新操作
BEGIN
SET @sqlstr = 'select @provepath=provepath from '+@tablename+' where UserID='+@userid + ' and MessageID='+@msgid
EXEC sp_executesql @sqlstr,
N'@provepath VARCHAR(1000) output',
@provepath OUTPUT
SET @provepath= REPLACE(@provepath,@filepath+',','')
SET @sqlstr = 'update '+@tablename+' set provepath = '''+@provepath+''''
IF @provepath = ''
BEGIN
SET @sqlstr +=',provered='+CAST(@NOT_PROVE AS VARCHAR(10))
END
SET @sqlstr +=' where UserID='+@userid + ' and MessageID='+@msgid
PRINT @sqlstr
EXEC(@sqlstr)
END
END