3
我想寫一個接受兩個輸入(內容ID和內容類型)的SQL查詢,根據用戶設置的內容從不同的源表填充臨時表作爲內容類型,然後最後對臨時表運行查詢。我想我會陷入將我的臨時表合併到我的IF/ELSE語句中。使用IF/ELSE並插入臨時表
所以我的問題是,爲什麼這回幾個非零數字結果:
DECLARE @contentID int, @contenttype varchar
SET @contentid = 28861
SET @contenttype = 'resource'
DECLARE @tags_current TABLE (contentID int, taxonomyID int, isPrimary int)
INSERT INTO @tags_current (contentID, taxonomyID, isPrimary)
SELECT resourceID as contentID, taxonomyID, isPrimary
FROM resource2taxonomy r2t
WHERE r2t.resourceID = @contentID
SELECT * FROM @tags_current
而這將返回結果爲零:
DECLARE @contentID int, @contenttype varchar
SET @contentid = 28861
SET @contenttype = 'resource'
DECLARE @tags_current TABLE (contentID int, taxonomyID int, isPrimary int)
IF (@contenttype = 'resource')
BEGIN
INSERT INTO @tags_current (contentID, taxonomyID, isPrimary)
SELECT resourceID as contentID, taxonomyID, isPrimary
FROM resource2taxonomy r2t
WHERE r2t.resourceID = @contentID
END
SELECT * FROM @tags_current
而且,我已經試過這一點,它也返回零個結果:
DECLARE @contentID int, @contenttype varchar, @command varchar
SET @contentid = 28861
SET @contenttype = 'resource'
DECLARE @tags_current TABLE (contentID int, taxonomyID int, isPrimary int)
IF (@contenttype = 'resource')
BEGIN
SET @command = 'INTO @tags_current (contentID, taxonomyID, isPrimary) SELECT resourceID as contentID, taxonomyID, isPrimary FROM resource2taxonomy r2t WHERE r2t.resourceID = @contentID'
END
EXECUTE(@command)
SELECT * FROM @tags_current
我對this讀了,this和this,但由於某種原因,它只是不爲我點擊。我使用MS SQL Server 2014,以防萬一。這是SQL Fiddle。謝謝!
謝謝你 - 這就是答案。對於我來說,接受它作爲答案還爲時過早(不能在10分鐘內接受答案),但我會回來做這件事。 – Scott