久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL 聲明變量

SQL Declare Variables(SQL 聲明變量)
本文介紹了SQL 聲明變量的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

誰能查看我的聲明...

Can anyone check on my statement...

DECLARE @tblName varchar(MAX), 
        @strSQL varchar(MAX)

SET @tblName ='SELECT DISTINCT o.name as TableName 
                 FROM sysobjects o 
                 JOIN sysindexes x on o.id = x.id  
                WHERE o.name LIKE ''%empty%'''  

SET @strSQL = 'INSERT INTO @tblName VALUES(''trylng'', ''1'')'
EXEC (@strSQL)

我的錯(cuò)誤是...

消息 1087,級(jí)別 15,狀態(tài) 2,第 1 行
必須聲明表變量@tblName".

Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "@tblName".

推薦答案

你的 @tblName 屬性存在于外部作用域——普通"代碼行的作用域——但不存在于內(nèi)部作用域您在那里的字符串中構(gòu)建的 SQL....

Your @tblName property exists at the outer scope - the scope of your "normal" code lines - but not at the inner scope of the SQL you're constructing in the string there....

您需要更改您的行以閱讀:

You need to change your lines to read:

SET @strSQL = 'INSERT INTO ' + @tblName + ' VALUES(''trylng'', ''1'')'

然后它應(yīng)該可以正常工作.

and then it should work just fine.

此外,您沒有提到您的 SQL Server 版本 - 但從 SQL Server 2005 或更新版本開始,您應(yīng)該停止使用 sysobjectssysindexes - 而是使用新的 sys 架構(gòu)包含或多或少相同的信息 - 但更容易獲得.將您的查詢更改為:

Also, you're not mentioning your SQL Server version - but as of SQL Server 2005 or newer, you should stop using sysobjects and sysindexes - instead, use the new sys schema that contains more or less the same information - but more easily available. Change your query to:

SET @tblName ='SELECT DISTINCT t.name as TableName 
               FROM sys.tables t
               INNER JOIN sys.indexes i on i.object_id = t.object_id  
               WHERE t.name LIKE ''%empty%'''  

請(qǐng)參閱 MSDN:查詢 SQL Server 系統(tǒng)目錄,了解更多信息有關(guān)新 sys 架構(gòu)中的可用內(nèi)容以及如何充分利用它的更多信息!

See MSDN: Querying the SQL Server System Catalog for a lot more information on what's available in the new sys schema and how to make the most of it!

正如 "rsbarro" 指出的那樣:將這里的 SQL 語句放在引號(hào)中很奇怪 - 您是否也在使用 EXEC(...) 執(zhí)行此語句??但是,您如何將值分配回 @tblName 屬性?真的沒有意義.....

As "rsbarro" pointed out : putting this SQL statement here into quotes is odd - are you executing this statement using EXEC(...), too?? But then how do you assign the value back to the @tblName property? Doesn't really make sense.....

如果你想實(shí)際運(yùn)行這個(gè)查詢來獲取一個(gè)值,你應(yīng)該有這樣的東西:

If you want to actually run this query to get a value, you should have something like this:

 SELECT TOP 1 @tblName = t.name
 FROM sys.tables t
 INNER JOIN sys.indexes i on i.object_id = t.object_id  
 WHERE t.name LIKE '%empty%'

您需要有一個(gè) TOP 1 才能確定只獲得一個(gè)值 - 否則此語句可能會(huì)失敗(如果選擇了多行).

You need to have a TOP 1 in there to be sure to get just a single value - otherwise this statement could fail (if multiple rows are selected).

這篇關(guān)于SQL 聲明變量的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問題和答案的組合))
主站蜘蛛池模板: 日韩中文一区二区三区 | 成人欧美一区二区三区在线观看 | 五月综合激情网 | 国产日韩久久久久69影院 | 日本一区二区影视 | 九九精品在线 | 最近中文字幕在线视频1 | 视频一二三区 | 亚洲国产精品久久久久秋霞不卡 | 三级欧美| 网色 | 免费中文字幕 | a级黄色片在线观看 | 精品人伦一区二区三区蜜桃网站 | 国产欧美精品一区二区 | 欧美日韩一二三区 | 国产精品久久久久久久久久久久 | 国产综合在线视频 | 天堂网中文字幕在线观看 | 啪啪免费| 国产成人精品999在线观看 | 成人在线小视频 | 伊人久麻豆社区 | 欧美不卡一区二区三区 | 久久一视频| 色综合天天网 | 亚洲在线| 亚洲成人精品久久久 | 亚洲精品成人av久久 | 欧美激情综合 | 亚洲精品日韩欧美 | 精品久久久999 | 久久一 | 一区二区三区免费观看 | 在线视频一区二区 | 欧美在线一区二区三区 | 一区二区不卡高清 | 久久中文字幕一区 | 色妹子综合网 | 欧美在线一区二区三区 | 欧美一区二区在线视频 |