December 4, 2006 at 7:50 am
How come you don't know the columns list at run time???
One way to debug this is to use the print statement :
Print @sql
Then tweak that code untill the print statement looks like it should run.
One last thing, you have to concatenate all the parameteres in the value part to make this code work. That is again why you should not be using dynamic sql .
December 4, 2006 at 7:51 am
This should be a good reading for you :
http://www.sommarskog.se/dynamic_sql.html
December 4, 2006 at 8:00 am
thanks for the info, the reason for the dynamic is because the data could be put in anyone of 10 different colomns
December 4, 2006 at 8:04 am
Insert into dbo.TableName (col1, col2, col10) values (@par1, @par2, @Par10).
Make sure to validate that non-nullable columns get feeded a value. Then all other params can be set to null.
December 4, 2006 at 8:15 am
I need to be able to pass the colomn name into the stored procedure that this insert query resides
December 4, 2006 at 8:21 am
Why? The columns name shouldn't change often in the life of the application!
Where are those Joe Celko quotes when you need them?!?!!
December 4, 2006 at 8:25 am
I have colomns called date1,john,george,joe,fred
I'd like to be able to insert data in colomns date1 and one of the other colomns, but by passing in the name of the colomn I want to insert into
December 4, 2006 at 8:36 am
Insert into Table (Date, John, George, Joe, Fred) VALUES (GetDate(), 'John', NULL, NULL, NULL)
Substitute the value part with parameters and that will always work.
Do you see anything wrong with this solution?
December 4, 2006 at 9:03 am
Other than a design problem, looks good
December 4, 2006 at 9:06 am
I already implied that... maybe I was too subtile .
December 4, 2006 at 9:06 am
many thanks for the assist
December 5, 2006 at 5:56 am
Can U send me a complete Query.
December 5, 2006 at 6:28 am
A complete query of what Bhudev?
December 5, 2006 at 7:13 am
HI I'M SENDING U SAMPLE QUERY - HOPE IT WILL WORK BUT THERE WILL BE ANOTHER PROBLEME WHEN U'LL CHANGE SECOND FIELD OTHER THAN INT. I'LL POST THAT SOLUTION TOMARROW. Till enjoy.
CREATE TABLE TEMP
(
Fdate DATETIME,
vInt INT
)
GO
SET DATEFORMAT DMY
DECLARE
@sEin NVARCHAR(128),
@sDate1 NVARCHAR(128),
@SQL NVARCHAR(500),
@integerval NVARCHAR(10)
SET @sEin = 'vInt'
SET @integerval = 100
SET @sDate1 = '25-10-2006'
SET @sql = 'INSERT INTO TEMP (fdate,' + @sEin + ') VALUES (''' + @sdate1 + ''',' + @integerval + ')'
EXEC ('
' + @sql + '
')
GO
SELECT * FROM TEMP
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply