Я получил ошибки в этих строках:
DROP TABLE ID@SP_TableName
INSERT INTO @SP_TableName
SELECT * FROM #temp
Требуется решение, если таблица существует, я хочу union
другие строки из B1_40 таблицы с CN_xxx таблица.
Моя хранимая процедура:
ALTER PROCEDURE sp_SplitByClaim
AS
-- separate rows by Claim Number
DECLARE @SP_ClaimNum NVARCHAR(10)
DECLARE @SP_TableName NVARCHAR(50)
DECLARE @SP_Statement NVARCHAR(300)
SET NOCOUNT ON
BEGIN
-- select the unique claim numbers first
DECLARE ClaimCursor40 CURSOR FOR
SELECT DISTINCT Claim_Num
FROM B1_40
ORDER BY Claim_Num
DECLARE ClaimCursor50 CURSOR FOR
SELECT DISTINCT Claim_Num
FROM B1_50
ORDER BY Claim_Num
DECLARE ClaimCursor70 CURSOR FOR
SELECT DISTINCT Claim_Num
FROM B1_70
ORDER BY Claim_Num
-- EXEC sp_dropClaimTable
PRINT 'Started ClaimCursor40'
OPEN ClaimCursor40
FETCH NEXT FROM ClaimCursor40 INTO @SP_ClaimNum
SET @SP_TableName = 'CN_' + @SP_ClaimNum
WHILE @@FETCH_STATUS = 0
BEGIN
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_CATALOG = 'Drawback'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = @SP_TableName))
BEGIN
DROP TABLE #temp
SET @SP_Statement = 'SELECT * INTO #temp FROM (' +
' SELECT * FROM CN_' + @SP_ClaimNum + ' UNION SELECT * FROM B1_40 WHERE Claim_Num = ' +@SP_ClaimNum + ')'
EXECUTE sp_executesql @SP_Statement
DROP TABLE ID@SP_TableName
INSERT INTO @SP_TableName
SELECT * FROM #temp
END
ELSE
BEGIN
SET @SP_Statement = 'SELECT * into dbo.CN_' + @SP_ClaimNum +
' FROM B1_40 WHERE Claim_Num =' + @SP_ClaimNum
EXECUTE sp_executesql @SP_Statement
END
FETCH NEXT FROM ClaimCursor40 INTO @SP_ClaimNum
END
CLOSE ClaimCursor40
DEALLOCATE ClaimCursor40
Чего мне не хватает?