我試圖從查詢返回的值(表名)中檢查是否有記錄,並且該記錄中的某些值爲空。如果他們是,那麼我想要將表名插入臨時表中。我得到一個錯誤:將動態SQL查詢的COUNT結果轉換爲INT
Conversion failed when converting the varchar value 'count(*)
FROM step_inusd_20130618 WHERE jobDateClosed IS NULL' to data type int.
這是查詢:
DECLARE @table_name VARCHAR(150)
DECLARE @sql VARCHAR(1000)
DECLARE @test int
SELECT @table_name = tableName FROM #temp WHERE id = @count
SET @sql = 'SELECT * FROM ' + @table_name + ' WHERE jobDateClosed IS NULL'
--ERROR is below:
select @test = 'count(*) FROM ' + @table_name + ' WHERE jobDateClosed IS NULL'
--PRINT @sql
-- EXEC(@sql)
IF @test > 0
BEGIN
INSERT INTO #temp2 (tablename) VALUES (@table_name);
END
SET @count = @count + 1
任何想法如何計數的結果轉換爲整數?
@test是INT。這會失敗.... –