johnsmith180
Programmer
Hi
I want to check the values of my parameters in my Stored Procedure and write the SQL statement accordingly.
I am writing Dynamic SQL and referencing a temporary Table in it, but I get an error that I must declare the @NewTable.
I have following code:
---------------------------------------------
CREATE PROCEDURE SearchAttributesTest
-- search for these attributes
@AttributeValueID1 int,
@AttributeValueID2 int,
@AttributeValueID3 int,
@AttributeValueID5 int,
@AttributeValueID6 int
AS
declare @RecordList table (RecordID int)
DECLARE @NewTable TABLE (productid nvarchar(50), valueforType1 int, valueForType2 int, valueForType3 int, valueForType5 int, valueForType6 int )
DECLARE @SQL NVarChar (4000),
@paramlist nvarchar(4000)
Select @SQL = 'SELECT ( p.ProductID), (p.ProductName)FROM @XNewTable nt inner join
Viamed_Products p on nt.productID = p.ProductID INNER JOIN
Viamed_Products_Categories cat ON p.ProductID = cat.ProductID INNER JOIN @XRecordList cr ON cat.CategoryID = cr.RecordID WHERE 1=1'
if (@AttributeValueID1 < 0)
Select @sql = @sql + ' AND nt.ValueForType1 = @XAttributeValueID1'
if @AttributeValueID2 < 0
Select @sql = @sql + ' AND nt.ValueForType2 = @XAttributeValueID2'
-- DO this for 5 attributes
SELECT @paramlist = '
@XNewTable int,
@XRecordList int,
@XAttributeValueID1 int,
@XAttributeValueID2 int,
@XAttributeValueID3 int,
@XAttributeValueID5 int,
@XAttributeValueID6 int '
--Return the result set
EXEC sp_executesql @sql, @paramlist, @NewTable, @RecordList @AttributeValueID1, @AttributeValueID2, @AttributeValueID3, @AttributeValueID5,
@AttributeValueID6
END
GO
------------------------------------
However, I get an error that I must declare the @NewTable.
I hope you can solve this problem
Regards
I want to check the values of my parameters in my Stored Procedure and write the SQL statement accordingly.
I am writing Dynamic SQL and referencing a temporary Table in it, but I get an error that I must declare the @NewTable.
I have following code:
---------------------------------------------
CREATE PROCEDURE SearchAttributesTest
-- search for these attributes
@AttributeValueID1 int,
@AttributeValueID2 int,
@AttributeValueID3 int,
@AttributeValueID5 int,
@AttributeValueID6 int
AS
declare @RecordList table (RecordID int)
DECLARE @NewTable TABLE (productid nvarchar(50), valueforType1 int, valueForType2 int, valueForType3 int, valueForType5 int, valueForType6 int )
DECLARE @SQL NVarChar (4000),
@paramlist nvarchar(4000)
Select @SQL = 'SELECT ( p.ProductID), (p.ProductName)FROM @XNewTable nt inner join
Viamed_Products p on nt.productID = p.ProductID INNER JOIN
Viamed_Products_Categories cat ON p.ProductID = cat.ProductID INNER JOIN @XRecordList cr ON cat.CategoryID = cr.RecordID WHERE 1=1'
if (@AttributeValueID1 < 0)
Select @sql = @sql + ' AND nt.ValueForType1 = @XAttributeValueID1'
if @AttributeValueID2 < 0
Select @sql = @sql + ' AND nt.ValueForType2 = @XAttributeValueID2'
-- DO this for 5 attributes
SELECT @paramlist = '
@XNewTable int,
@XRecordList int,
@XAttributeValueID1 int,
@XAttributeValueID2 int,
@XAttributeValueID3 int,
@XAttributeValueID5 int,
@XAttributeValueID6 int '
--Return the result set
EXEC sp_executesql @sql, @paramlist, @NewTable, @RecordList @AttributeValueID1, @AttributeValueID2, @AttributeValueID3, @AttributeValueID5,
@AttributeValueID6
END
GO
------------------------------------
However, I get an error that I must declare the @NewTable.
I hope you can solve this problem
Regards