0

I have a stored procedure written to insert a new row to a table and I wish to generalize it so a single procedure may add rows to any of a range of tables depending on the input. Here is the query which presently defines the individual case:

CREATE PROCEDURE [Test].[NO001CreateRow]
@InstanceID char(5)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.NO001 (InstanceID)
VALUES (@InstanceID)
END
GO

This seems to work fine. InstanceID is the name of a key-value column in the table NO001. The procedure inserts a new row into NO001 with the value of InstanceID set to the input @InstanceID.

Here is the query which defines the procedure I have made for the general case:

CREATE PROCEDURE [Test].[GenCreateRow]
@TableID nchar(5),
@InstanceID nchar(5)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql nvarchar(max) = 'INSERT INTO dbo.' + @TableID + ' (InstanceID)
VALUES (' + @InstanceID + ')'
EXEC sp_executesql @sql
END
GO

As far as I can tell, this procedure should add to a table identified by the input five-character @TableID a new row with column InstanceID populated by the input @InstanceID. However, when I run the procedure with parameters @TableID=NO001 and @InstanceID=XXXXX the code breaks and returns the error message Invalid column name 'XXXXX'.

I do not understand why it tries to find a column in table @TableID titled @InstanceID. I understood the column definition to be the parenthetical InstanceID on line 7, and I don't believe InstanceID should automatically assume the value of @InstanceID. Does anyone know what's going on here?

Here is the code displayed on execution:

DECLARE @return_value int
EXEC @return_value = [Test].[GenCreateRow]
@FormID = N'NO001',
@InstanceID = N'XXXXX'
SELECT 'Return Value' = @return_value
GO
2
  • Have you tried it without sp_executesql? Commented Aug 6, 2014 at 16:35
  • Change EXEC to PRINT and have a look. Might just be missing quotes around your value strings. Commented Aug 6, 2014 at 16:36

2 Answers 2

3

InstatnceID is a nchar, so in the values you need quotes denoting a string value in the command you are putting together.

 DECLARE @sql nvarchar(max) = 'INSERT INTO dbo.' + @TableID + ' (InstanceID) VALUES (''' + @InstanceID + ''')'

it makes your code you run from this

INSERT INTO dbo.TableName (InstanceID) VALUES (IDVALUE)

to

INSERT INTO dbo.TableName (InstanceID) VALUES ('IDVALUE') 

when you execute it.

Sign up to request clarification or add additional context in comments.

Comments

2

Try to add double apostrophe

CREATE PROCEDURE [Test].[GenCreateRow]
@TableID nchar(5),
@InstanceID nchar(5)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql nvarchar(max) = 'INSERT INTO dbo.' + @TableID + ' (InstanceID)
VALUES (''' + @InstanceID + ''')'
EXEC sp_executesql @sql
END
GO

It works because your code do this:

INSERT INTO dbo.Table  (InstanceID) VALUES (XXX)

and my code do this:

INSERT INTO dbo.Table  (InstanceID) VALUES ('XXX')

1 Comment

@Bibliophael I've added explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.