新聞中心
動態(tài)sql語句功能很強大,可以實現(xiàn)許多我們需要的功能,下面就為您介紹一些動態(tài)sql語句基本語法方面的知識,供您參考學(xué)習(xí)。

為扶風(fēng)等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及扶風(fēng)網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、做網(wǎng)站、扶風(fēng)網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
1 :普通SQL語句可以用Exec執(zhí)行 eg:
Select * from tableName Exec('select * from tableName') Exec sp_executesql N'select * from tableName' -- 請注意字符串前一定要加N
2:字段名,表名,數(shù)據(jù)庫名之類作為變量時,必須用動態(tài)sql語句eg:
declare @fname varchar(20) set @fname = 'FiledName' Select @fname from tableName -- 錯誤,不會提示錯誤,但結(jié)果為固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 請注意 加號前后的 單引號的邊上加空格 當(dāng)然將字符串改成變量的形式也可
declare @fname varchar(20) set @fname = 'FiledName' --設(shè)置字段名
declare @s varchar(1000) set @s = 'select ' + @fname + ' from tableName' Exec(@s) -- 成功 exec sp_executesql @s -- 此句會報錯
declare @s Nvarchar(1000) -- 注意此處改為nvarchar(1000) set @s = 'select ' + @fname + ' from
tableName' Exec(@s) -- 成功
exec sp_executesql @s -- 此句正確
3. 輸出參數(shù)
declare @num int, @sqls nvarchar(4000) set @sqls='select count(*) from tableName' exec(@sqls)
--如何將exec執(zhí)行結(jié)果放入變量中?
declare @num int, @sqls nvarchar(4000) set @sqls='select @a=count(*) from tableName ' exec sp_executesql @sqls,N'@a int output',@num output select @num
網(wǎng)頁名稱:動態(tài)sql語句基本語法介紹
網(wǎng)頁URL:http://www.fisionsoft.com.cn/article/cdhodig.html


咨詢
建站咨詢
