我想教我自己都SQL項目和sp_executesql我正在工作的項目。我想,我很接近,但我沒有得到我想要的,我希望有人能幫助我度過自己缺乏的知識。在此先感謝您的幫助!努力學習SQL Server中的sp_executesql
以下是我當前的代碼(註釋的註解我對自己說):
DECLARE @form_id INT
DECLARE @element_table NVARCHAR(50)
DECLARE @element_column NVARCHAR(50)
DECLARE @element_id INT
DECLARE @sqlString NVARCHAR(1000)
DECLARE @sqlDefinition NVARCHAR(1000)
DECLARE @outputDataType VARCHAR(50)
DECLARE @parmRET1 VARCHAR(30)
DECLARE @sqlresult VARCHAR(50)
DECLARE @passed_element_table NVARCHAR(50)
DECLARE @passed_element_column NVARCHAR(50)
SET @form_id = 220 --- temporary value for testing
--- this gets the list of ELEMENT_IDs for the cursor
DECLARE db_cursor CURSOR FOR SELECT
ELEMENT_ID
FROM FORM_ELEMENT fe
WHERE fe.FORM_ID = @form_id
--- Cursor loop starts here
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @element_id
WHILE @@fetch_status = 0
BEGIN
--- These two SET statements get me the table name and column I want to retrieve the datatype for
SET @element_table = (
SELECT DISTINCT
mtm.MAP_TABLE_NAME
FROM FORM_ELEMENT fe
LEFT OUTER JOIN ELEMENT e
ON fe.ELEMENT_ID = e.ELEMENT_ID
LEFT OUTER JOIN FORM_DATA_MAPPING_MASTER fdmm
ON fe.FORM_DATA_MAPPING_ID = fdmm.FORM_DATA_MAPPING_ID
LEFT OUTER JOIN MAPPING_TABLE_MASTER mtm
ON fdmm.MAP_TABLE_ID = mtm.MAP_TABLE_ID
INNER JOIN FORM f
ON fe.FORM_ID = f.FORM_ID
WHERE fe.FORM_ID = @form_id
AND fe.ELEMENT_ID = @element_id
)
SET @element_column = (
SELECT DISTINCT
mcm.MAP_COLUMN_NAME
FROM FORM_ELEMENT fe
LEFT OUTER JOIN ELEMENT e
ON fe.ELEMENT_ID = e.ELEMENT_ID
LEFT OUTER JOIN FORM_DATA_MAPPING_MASTER fdmm
ON fe.FORM_DATA_MAPPING_ID = fdmm.FORM_DATA_MAPPING_ID
LEFT OUTER JOIN MAPPING_COLUMN_MASTER mcm
ON fdmm.MAP_COLUMN_ID = mcm.MAP_COLUMN_ID
WHERE fe.FORM_ID = @form_id
AND fe.ELEMENT_ID = @element_id
)
--- This is where I begin building the dynamic sql. I'm unsure if I need the DECLARE statement here, if I've used it above
SET @sqlString = N'DECLARE @sqlResult VARCHAR(50) SET @sqlResult = (SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @passed_element_table AND COLUMN_NAME = @passed_element_column)'
SET @sqlDefinition = N'@passed_element_table nvarchar(50), @passed_element_column nvarchar(50), @outputDataType varchar(30) OUTPUT'
SET @passed_element_table = @element_table
SET @passed_element_column = @element_column
--- This is where I execute the dynamic sql
EXECUTE sp_executesql @sqlString
, @sqlDefinition
, @passed_element_table = @element_table
, @passed_element_column = @element_column
, @outputDataType = @sqlresult OUTPUT
--- Here I'm just using a print statement to dump out the results of the prior SP execution
PRINT @element_table + ' | ' + @element_column + ' | ' +
CASE
WHEN @outputDataType IS NULL THEN ''
ELSE @outputDataType
END
--- This ends the loop for this element_id, and restarts the process
FETCH NEXT FROM db_cursor INTO @element_id
END
--- This finishes the cursor loop, and then deallocates the cursor when there are no more element_ids
CLOSE db_cursor
DEALLOCATE db_cursor
查詢沒有錯誤(勝利)中運行,但它似乎並沒有得到任何值回來自sp_executesql子例程。下面是返回的輸出樣本:
BIOGRAPH_MASTER | BIRTH_DTE |
BIOGRAPH_MASTER | SSN |
BIOGRAPH_ETHNIC_RACE | RACE |
BIOGRAPH_ETHNIC_RACE | ETHNICITY |
NAME_MASTER | UDEF_1A_2 |
ADDRESS_MASTER | ADDR_LINE_1 |
ADDRESS_MASTER | ADDR_LINE_2 |
ADDRESS_MASTER | CITY |
ADDRESS_MASTER | STATE |
我敢肯定,我有我如何建造的@SQLString發送到sp_executesql的錯誤,但我不能找到一個很好的資源,以幫助我明白我出錯的地方。正如我之前所說,任何援助將不勝感激。
由於您使用遊標掙扎,你應該忘掉你所學到的和離開他們。從性能的角度來看,遊標是可怕的。幾乎總是有一套基於一套方法來完成大多數事情。如果你可以解釋你在這裏做什麼,我會很樂意幫你找到一個解決方案,而不使用遊標。 –