I am trying to match a small record set (500 records) with a huge dataset on a linked server...the linked server tables have about 2 million records so i want all the processing to occur remotely and bring back all of the records only when matched..
I have a sql script and am trying to use a table variable but I keep getting an error message when i try to do this...
Do variables in dynamic sql like this only work with scalar variables?
Error msg:
Msg 137, Level 16, State 1, Line 29
Must declare the scalar variable "@member".
DECLARE @member TABLE(
memberid VARCHAR(30) NOT NULL
)
INSERT INTO @member
SELECT memberid
FROM [Reportingdb].dbo.SMALL_TABLE
DECLARE @sql VARCHAR(MAX)
SET @sql = '
SELECT * FROM OPENQUERY ( [LINKED_SRV], ''
SELECT
m.ID,
p.NAME,
m.MEMBER_NUMBER
FROM [database].[dbo].MEMBER m
INNER JOIN [database].[dbo].CUSTOMER p
ON m.ID = p.ID
INNER JOIN '''''+@member+''''' t
t.memberid = m.MEMBER_NUMBER
GROUP BY
m.ID,
p.NAME,
m.MEMBER_NUMBER
)'
exec (@sql)
↧