日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
為您講解SQL動(dòng)態(tài)語(yǔ)句的語(yǔ)法

動(dòng)態(tài)SQL語(yǔ)句是SQL中相當(dāng)重要的語(yǔ)句,下面就將為您詳細(xì)介紹動(dòng)態(tài)SQL語(yǔ)句的語(yǔ)法,供您參考,希望對(duì)您能夠有所啟示。

1 :普通SQL語(yǔ)句可以用Exec執(zhí)行

eg:   Select * from tableName
         Exec('select * from tableName')
         Exec sp_executesql N'select * from tableName'    -- 請(qǐng)注意字符串前一定要加N

2:字段名,表名,數(shù)據(jù)庫(kù)名之類作為變量時(shí),必須用動(dòng)態(tài)SQL

eg:  
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName              -- 錯(cuò)誤,不會(huì)提示錯(cuò)誤,但結(jié)果為固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName')     -- 請(qǐng)注意 加號(hào)前后的 單引號(hào)的邊上加空格

當(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   -- 此句會(huì)報(bào)錯(cuò)

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


當(dāng)前名稱:為您講解SQL動(dòng)態(tài)語(yǔ)句的語(yǔ)法
網(wǎng)頁(yè)路徑:http://m.5511xx.com/article/dpjpsgj.html