Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Exec Dynamic Insert Statement Error on Commas?

Posted on 2014-01-22
5
Medium Priority
?
405 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
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 800 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 800 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

963 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