有時候需要執行動態拼接的sql,這就用到了動態語句
exec 執行傳入變數可以,但是如果想傳出引數,需要用到sp_executesql 並且注意引數需要是國家字符集
set quoted_identifier off
set @v_sql='select @n_num=count(1),@v_val=sum(dvalue) from '+@tname+ ' where id='+@staid
exec sp_executesql @v_sql,
n'@n_num numeric output,@v_val numeric output',
@n_num output,@v_val output
-- 在這裡使用變數@n_num,@v_val的值
set quoted_identifier on
但是在返回游標的動態sql中不支援,我們可以使用全域性臨時表解決
if object_id('tempdb..##gt__data_tmp',n'u') is not null
drop table ##gt_data_tmp
set @v_sql = @str + ' into ##gt_data_tmp from ***'
execute(@v_sql)
MSSQL經典語句
1.按姓氏筆畫排序 select from tablename order by customername collate chinese prc stroke ci as 2.資料庫加密 select encrypt 原始密碼 select pwdencrypt 原始密碼 select pwdco...
MSSQL經典語句
1.按姓氏筆畫排序 select from tablename order by customername collate chinese prc stroke ci as 2.資料庫加密 select encrypt 原始密碼 select pwdencrypt 原始密碼 select pwdco...
MSSQL備份語句
完整備份 backup database northwindcs to disk g backup northwindcs full 20070908.bak 差異備份 backup database northwindcs to disk g backup northwindcs diff 200...