I am trying to build a dynamic query that will insert or update a record based on row exists in db, if yes i will update a bunch of records & sub records depending on @ObjectID.
Here is my query:
DECLARE @ObjectID BIGINT = 0;
SET @ObjectID = 0;
IF NOT EXISTS (SELECT ID
FROM dbo.ResortInfo
WHERE dbo.ResortInfo.resortCode = N'PYI')
BEGIN
INSERT INTO dbo.ResortInfo (columns)
VALUES (colvalues)
SET @ObjectID = SCOPE_IDENTITY()
PRINT @ObjectID
END
ELSE
BEGIN
PRINT 'Already exists' -- update query will replace here
END
The query runs ok without the declare part, but when I add
DECLARE @ObjectID BIGINT = 0;
SET @ObjectID = 0;
I get the following error :
Msg 208, Level 16, State 1, Line 4
Invalid object name 'dbo.ResortInfo'