Error:
I get an error on SELECT * FROM @SportsTable
reporting I must declare the scalar variable. I don't understand, if it is declared as a paramter to the stored procedure should I not be able to use it? If I remove the If NOT EXISTS
line the procedure compiles correctly.
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_Sports_Insert]
(
@SportsTable dbo.TVP_SportsTable READONLY
)
AS
if NOT EXISTS (SELECT SportsTable.SportGUID FROM SportsTable WHERE SportsGUID = @SportsTable.SportsGUID)
BEGIN
INSERT INTO [dbo].[SportsTable]
SELECT * FROM @SportsTable
END
Objective:
To pass a table from Visual Studio 2008 to the parameter TVP_SportsTable. Then to insert any rows that do not already match an existing row in the [dbo].SportsTable. If an incoming row in the TVP_SportsTable matches an existing row in the [dbo].SportsTable, the row should be ignored, the remaining should be inserted.
*NOTE: TVP_SportsTable references a User defined type, it is defined on the server with the same structure as the dbo.TVP_SportsTable*
SELECT
statement without appearing in theFROM
or as aJOIN
-ed table and it could have more than one row anyway. Some of which do exist and others which don't. – Martin SmithIF
line. I am able to compile the stored procedure. So I don't see how theIF
takes the variable out of scope. – Ccorock@SportsTable
has three rows. Two of them match an existing row in[dbo].[SportsTable]
and one doesn't then what is the desired logic? Insert the one row and ignore the other two? Insert the one row and update the other two? Skip inserting anything at all? Something else? – Martin Smith