Solved

SQL Exec Dynamic Insert Statement Error on Commas?

Posted on 2014-01-22
5
400 Views
Last Modified: 2014-01-23
I'm not sure if this is possible, but I need to build an Insert statement with added variable column names. I'm testing with only one column name here. I get this error message but don't see how the commas are the problem. Help!

 Error: " Incorrect syntax near ',' "

Declare @Columns    varchar(30) = '[Test - Column]';
Declare @State           varchar(10) = 'NV';
Declare @ListName   varchar(10) = 'List1';
Declare @Updated    datetime = null;
Declare @Price           decimal(18,2) = 2.00;

Exec('Insert Into dbo.MyInsertTest ( [State], ListName, Updated,' + @Columns +')
 	Values(' + @State + ', ' + @ListName + ', ' + @Updated + ',' + @Price);

Open in new window

0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 200 total points
ID: 39801968
The varchar values need to be enclosed in quotes in the resulting sql string to be executed. Date field's a bit tricky. It usually helps to build a string and display it first.
Declare @sql varchar(max) = 'Insert Into dbo.MyInsertTest ( [State], ListName, Updated,' + @Columns +')
	Values(''' + @State + ''', ''' + @ListName + ''', ' + 
	case when @Updated is null then 'null' else '''' + convert(varchar, @updated, 126) + '''' end + ', ' + 
	convert(varchar, @Price) + ')';

print @sql;

Exec(@sql);

Open in new window

0
 
LVL 3

Assisted Solution

by:smilieface
smilieface earned 200 total points
ID: 39801996
Try this.

When concatenating strings you have to convert the data types explicitly.

This will also handle putting NULL in the date properly, but not for any other column.

Declare @Columns    varchar(30) = '[Test - Column]';
Declare @State      varchar(10) = 'NV';
Declare @ListName   varchar(10) = 'List1';
Declare @Updated    datetime    = NULL;
Declare @Price      decimal(18,2) = 2.00;

DECLARE @Cmd        varchar(max)

SET @Cmd = 'Insert Into dbo.MyInsertTest ( [State], ListName, Updated,' + @Columns 
   + ') Values(''' + @State + ''', ''' + @ListName + ''', '
   + ISNULL('''' + CONVERT(VARCHAR(25), @Updated, 121) + '''', 'NULL') + ','
   + CONVERT(VARCHAR(20), @Price) + ')';

--PRINT @Cmd

EXEC (@Cmd)

Open in new window


Edit - Bah! Beaten to the punch!
0
 

Author Comment

by:WorknHardr
ID: 39803534
Curious if any advantage between these two statements other than one is slightly shorter in statement length?

1) case when @Updated is null then 'null' else '''' + convert(varchar, @updated, 126) + '''' end + ', ' +
      convert(varchar, @Price) + ')';

2) ISNULL('''' + CONVERT(VARCHAR(25), @Updated, 121) + '''', 'NULL') + ','
   + CONVERT(VARCHAR(20), @Price) + ')';
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39803576
mine (1) is longer but avoids concatenation into a NULL value and checking only afterwards.

Another difference is the date format. I used 126 because that should work with any regional settings. If 121 is US date (I didn't check) it could fail on other systems like UK.
0
 

Author Closing Comment

by:WorknHardr
ID: 39804627
thx a million + 1
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question