Solved

SQL Exec Dynamic Insert Statement Error on Commas?

Posted on 2014-01-22
5
383 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now