DECLARE @TABLE VARCHAR(30)
SELECT TOP 99.NAME INTO #TEST FROM SYS.TABLES WHERE NAME=@TABLE
DECLARE MYCURSOR CURSOR FOR
SELECT NAME FROM #TEST
OPEN MYCURSOR
FETCH NEXT FROM MYCURSOR INTO @TABLE
WHILE @@FETCH_STATUS=0
BEGIN
SELECT * FROM @TABLE
FETCH NEXT FROM MYCURSOR INTO @TABLE
END
CLOSE MYCURSOR
DEALLOCATE MYCURSOR
DROP TABLE #TEST
如題,請問如果我想一次性搜尋整個資料庫table 所有欄位,目前@table 是字串,但該如何轉換成 資料表?
如果我宣告@table 為 table,請問該如何塞值呢?
如題,請問如果我想一次性搜尋整個資料庫table 所有欄位,目前@table 是字串,但該如何轉換成 資料表?
SELECT
TABLE_NAME
,COLUMN_NAME
,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
如果我宣告@table 為 table,請問該如何塞值呢?
SELECT * FROM @TABLE 改成 EXEC ('SELECT * FROM ' + @TABLE)
感謝大神的幫助
抱歉可能我當初說不清楚
是否可以用cursor 或其他方式 一次性執行類似於這樣
Select * From Table1
Select * From Table2
Select * From Table3
Select * From Table4
當初構想是將sys.tables裡面的name放入 #test
用#test的column當成table 逐個執行
因為table有97個,總不能一行一行執行,覺得重工
DECLARE @TABLE VARCHAR(30)
CREATE TABLE #TmepTable (
tb VARCHAR(50)
)
INSERT INTO #TmepTable SELECT NAME FROM SYS.TABLES
DECLARE @tb VARCHAR(50)
DECLARE XX CURSOR FOR
SELECT tb FROM #TmepTable
OPEN XX
FETCH NEXT FROM XX into @tb
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('SELECT * FROM ' + @tb)
FETCH NEXT FROM XX into @tb
END
CLOSE XX ;
DEALLOCATE XX;
DROP TABLE #TmepTable