473,395 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Select Into Stored Procedure

Hello

I have many different tables, that most have the same structure.. they all
have some primary key, some fields and mylinkid ... so what I want to make
is to create a stored procedure, where I can pass the table-name, primary
key, and the new mylinkid, what should happen is, that the procedure copies
the entrie, where i passed the id, to a new row and the mylink-id should get
the new value...

example:
mytable 1
id=1 (primary key autoincrement)
mylinkid=233
field1=asdf
field2=blabla

i call my procedure like: duplicate_entry 'mytable',1,4

the result should then be
id=2 (primary key/autincrement)
mylinkid=4
field1=asdf
field2=blabla

I was thinking to make a Stored Procedure, that copies the entier row with a
select * into mytable (SELECT * from mytable where id=1) and afterwards i
update the mylinkid ..... but there i have problem with the primary key.

The other solution that will work, but I won't like is to get with the
sp_fields all fields from that specific table, build the select into with
the correct fields without the id-field... that should work, but was asking
myself if there is somethign better...

Has someone a good idea for that?

Thanks
Jul 20 '05 #1
6 20149
I could do this in a DTS package without much difficulty. In an SP, the
main problem is using a variable tablename in an SQL statement is not
possible, as far as I know.
A solution could be using a CASE statement and using your tablename to
go to the right INSERT. Since you claim all tables are structural
identical copying those statements to build the CASE would be quick.

The INSERT would be something like this:

INSERT INTO MYTABLE (mylinkid,field1,field2)
SELECT @Mynewlinkid, field1, field2
FROM mytable
WHERE ID = @Keyfield

One wonders ofcourse why you have split an entity in different tables.

Hth,

Hans Brouwer

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #2
yes, i know that works, but the problem is, that i don't know the sctructure
of the tables, they can have field1, field2 but can also have 20 fields...
so this should be built dynamicly, i can make a stored-procedure that takes
out all fields from that table and makes that. the handling of the different
table is the smallest problem, i just pass the table_name as varchar and
then i build a string and run that with EXEC ... :-)

"hansje" <ha*****@anonymous.com> schrieb im Newsbeitrag
news:3f*********************@news.frii.net...
I could do this in a DTS package without much difficulty. In an SP, the
main problem is using a variable tablename in an SQL statement is not
possible, as far as I know.
A solution could be using a CASE statement and using your tablename to
go to the right INSERT. Since you claim all tables are structural
identical copying those statements to build the CASE would be quick.

The INSERT would be something like this:

INSERT INTO MYTABLE (mylinkid,field1,field2)
SELECT @Mynewlinkid, field1, field2
FROM mytable
WHERE ID = @Keyfield

One wonders ofcourse why you have split an entity in different tables.

Hth,

Hans Brouwer

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #3

"Patrick" <pa********@bluemail.ch> wrote in message
news:10***************@fuchs.cyberlink.ch...
Hello

I have many different tables, that most have the same structure.. they all
have some primary key, some fields and mylinkid ... so what I want to make
is to create a stored procedure, where I can pass the table-name, primary
key, and the new mylinkid, what should happen is, that the procedure copies the entrie, where i passed the id, to a new row and the mylink-id should get the new value...

example:
mytable 1
id=1 (primary key autoincrement)
mylinkid=233
field1=asdf
field2=blabla

i call my procedure like: duplicate_entry 'mytable',1,4

the result should then be
id=2 (primary key/autincrement)
mylinkid=4
field1=asdf
field2=blabla

I was thinking to make a Stored Procedure, that copies the entier row with a select * into mytable (SELECT * from mytable where id=1) and afterwards i
update the mylinkid ..... but there i have problem with the primary key.

The other solution that will work, but I won't like is to get with the
sp_fields all fields from that specific table, build the select into with
the correct fields without the id-field... that should work, but was asking myself if there is somethign better...

Has someone a good idea for that?


I will work if you break it up into sections beginning

if mytable = 'somespecifictable'
begin
put your specific CODE here
end

You may be able to script this specific code
into the source proc if the number of tables is large.


Jul 20 '05 #4
I am not sure then whether I understand your problem. If I understand
correctly, you can generate the right INSERT statement, for instance by
using SYSOBJECTS and SYSCOLUMNS.
You can add a new record, which is a duplicate of a selected record,
exept for the key. Now you wish to change the field MYLINKID; to do
this you can retrieve the value of the last inserted record with
@@IDENTITY(see BOL) and next update this record with your new MYLINKID.

Have I got this right, or do I still miss something?

Tnx,

Hans Brouwer

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
no you're right. That's the option i see .. :-) but was looking for a better
one... instead of generating the insert statement..
"hansje" <ha*****@anonymous.com> schrieb im Newsbeitrag
news:3f*********************@news.frii.net...
I am not sure then whether I understand your problem. If I understand
correctly, you can generate the right INSERT statement, for instance by
using SYSOBJECTS and SYSCOLUMNS.
You can add a new record, which is a duplicate of a selected record,
exept for the key. Now you wish to change the field MYLINKID; to do
this you can retrieve the value of the last inserted record with
@@IDENTITY(see BOL) and next update this record with your new MYLINKID.

Have I got this right, or do I still miss something?

Tnx,

Hans Brouwer

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #6
yes, but the main idea is, that i don't have to know how the tables look
like. because its always like that, they have a primary key, an mylinkid and
i want to copy it always on the same way. so users can create another table
in the same format, and just use that stored procedure to duplicate the
content with another mylinkid
"mountain man" <hobbit@southern_seaweed.com.op> schrieb im Newsbeitrag
news:_1********************@news-server.bigpond.net.au...

"Patrick" <pa********@bluemail.ch> wrote in message
news:10***************@fuchs.cyberlink.ch...
Hello

I have many different tables, that most have the same structure.. they all have some primary key, some fields and mylinkid ... so what I want to make is to create a stored procedure, where I can pass the table-name, primary key, and the new mylinkid, what should happen is, that the procedure copies
the entrie, where i passed the id, to a new row and the mylink-id should

get
the new value...

example:
mytable 1
id=1 (primary key autoincrement)
mylinkid=233
field1=asdf
field2=blabla

i call my procedure like: duplicate_entry 'mytable',1,4

the result should then be
id=2 (primary key/autincrement)
mylinkid=4
field1=asdf
field2=blabla

I was thinking to make a Stored Procedure, that copies the entier row with a
select * into mytable (SELECT * from mytable where id=1) and afterwards

i update the mylinkid ..... but there i have problem with the primary key.

The other solution that will work, but I won't like is to get with the
sp_fields all fields from that specific table, build the select into with the correct fields without the id-field... that should work, but was

asking
myself if there is somethign better...

Has someone a good idea for that?


I will work if you break it up into sections beginning

if mytable = 'somespecifictable'
begin
put your specific CODE here
end

You may be able to script this specific code
into the source proc if the number of tables is large.


Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jamiemcc | last post by:
Hi, I would like to have 1 stored procedure call another stored procedure (which contains multiple select statements) and then be able to access the 3 result sets. Example Create Procedure ....
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
6
by: Terentius Neo | last post by:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a select statement? I mean something like this: Select c.number, call procedure( c.number ) as list from table c With best...
3
by: nandan | last post by:
Hi, Has any one ever compared the performance of calling a DataTable's Select method with a stored procedure doing the same thing? My point is: dataRows = DataTable.Select(filter) is better or...
7
by: Matt Jensen | last post by:
Howdy Fairly simple question I think, I presume the answer is no it can't be reused for 2 *SELECT* statements, but just hoping for clarification. Just asking in the interests of trying to minimise...
1
by: anilcool | last post by:
Hi all, This is probably a simple problem for most of you.. Let me know if you have any pointers for me. I am new to DB2. In my stored procedure I want to select records that match a range of...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add...
4
by: vertigo262 | last post by:
Is it possible to use to select statements in a stored procedure? I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings. The code...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.