I need to have a generated query:
SELECT @numberOfRecords = COUNT(*) FROM Car WHERE CarId not in (Select CarId from CarData)
At the moment I have :
DECLARE @sql NVARCHAR(255)
SET @sql='Select CarId from CarData'
DECLARE @numberOfRecords BIGINT
SELECT @numberOfRecords = COUNT(*) FROM Car WHERE CarId not in (@sql)
I was trying other various combinations, like '(' nut it doesn’t work.
What I need to change to have it working?
What you are looking for is called dynamic sql, there is numerous articles about it, you’ll need to use sp_executesql if you are using SQL Server
there is an example here for something similar to what you are trying to do
your solution should look something like this