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*