Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sky
Starting Member
10 Posts |
Posted - 2002-01-30 : 09:06:03
|
| HiI want to write a stored procedure that will duplicate a row in a table, only replacing the unique key.Only the twist is that the table may gain extra columns over time and I don't want this to break the query.so its got to be dynamic... any takers...If your swimming upstream, your going the wrong way. |
|
|
jackstow
Posting Yak Master
160 Posts |
|
|
sky
Starting Member
10 Posts |
Posted - 2002-01-30 : 10:01:16
|
| I think there is an easier way...I need to combine a query like this INSERT INTO tTempTest (col1, col2, col3)SELECT col1, col2, col3FROM tTempTestWHERE id = 2only I want to make the selection dynamic...I could do this using a select like the following which returns column namesselect * from INFORMATION_SCHEMA.tables where table_name = 'tTempTest'" But how do I combine the two into one stored procedure, How do I use the results from the first select in the insert?And how do I ignore the id column?This is as far as I've got...BTW. I want to duplicate the row, because it is stuff like referral details for a shopping basket, after someone has made a purchase I need a new basket id, which is the key on this table, but I want to keep the old row and keep referral details on the new basket... messy I know but I haven't got time to redesign an entire database...------------------------------------If your swimming upstream, your going the wrong way.Edited by - sky on 01/30/2002 10:03:43 |
 |
|
|
sky
Starting Member
10 Posts |
Posted - 2002-01-30 : 12:40:29
|
| ok this is what i've got... anyone know how to make the bits in brackets work? perhaps by putting the select statement in a nested procedure?CREATE PROCEDURE spTempTest ASSelect COLUMN_NAMEFrom INFORMATION_SCHEMA.ColumnsWhere TABLE_NAME = 'tTempTest'Declare @columnText nvarchar(1000)<above select stement.eof=false.movenext>while <above select stement.eof=false> Set @columnText=@columnText + <above select stement("COLUMN_NAME")> + ',' <above select stement.eof=false.movenext>nextSet @columnText=left(@columnText,len(@columnText)-1)Declare @sqlText nvarchar(2000)Set @sqlText ='Insert into tTempTest ('Set @sqlText=@sqlText + @columnTextSet @sqlText=@sqlText +') Select 'Set @sqlText=@sqlText + @columnTextSet @sqlText=@sqlText +' From tTempTest Where id=2'exec(@sqlText)--------------------------------If your swimming upstream, your going the wrong way. |
 |
|
|
jackstow
Posting Yak Master
160 Posts |
Posted - 2002-01-31 : 05:22:01
|
| OK, what about this;DECLARE @intCounter intDECLARE @colCount intDECLARE @sqlText nvarchar(255)DECLARE @columnText nvarchar(255)DECLARE @columnName nvarchar(255)--this presumes atuo incrementing primary key is first column in the tableSET @intCounter = 2Select @colCount = count(COLUMN_NAME)From INFORMATION_SCHEMA.ColumnsWhere TABLE_NAME = 'tTempTest'SET @columnText = ''WHILE @intCounter <= @colCountBEGINSelect @columnName = COLUMN_NAMEFrom INFORMATION_SCHEMA.ColumnsWhere ORDINAL_POSITION = @intCounterAND TABLE_NAME = 'tTempTest' SET @columnText = @columnText + @columnName + ','SET @intCounter = @intCounter + 1ENDSet @columnText=left(@columnText,len(@columnText)-1)SET @sqlText = 'INSERT INTO tTempTest SELECT ' + @columnText + ' FROM tTempTest WHERE id = 2'EXEC (@sqlText)Jack..just found this article as well which might help -http://www.sqlteam.com/item.asp?ItemID=2971Edited by - jackstow on 01/31/2002 05:28:59 |
 |
|
|
sky
Starting Member
10 Posts |
Posted - 2002-02-04 : 04:46:59
|
Thanks Jack,That works a treat... and I understand what its doing,Cheers --------------------------------If your swimming upstream, your going the wrong way. |
 |
|
|
|
|
|
|
|