Solved

StoreProcedure select and Insert

Posted on 2007-12-02
26
463 Views
Last Modified: 2012-05-05
Hallo all..

I have Table A.
           Table B.
            Table C.
            Table D.
            Table X.

Each of them have columns "ID","Temp2","Temp3","Temp4","Temp5","Temp6"

I need a store procedure that will extract all the rows from Table x and insert them in
Table A, B, C and D ,, taking these tables as variables
Source table x destination table A..D

Thanks in Advance
0
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
  • 5
  • +1
26 Comments
 
LVL 13

Expert Comment

by:sonicefu
ID: 20394112

create or replace procedure transfer_data (src varchar2)
is
begin
  execute immediate 'insert into X select * from '||src;
  commit;
 exception
  when others then
        null;
end;

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20394130
Hi,

  you posted in MS SQL Server, Oracle AND MySQL zone(s), please specify which database you are actually using.
sonicefu posted some Oracle code, altough I don't think that is what you are looking for...
0
 

Author Comment

by:ZURINET
ID: 20394142
Sorry Guys..
I am looking for Microsoft SQL 2000 solution
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 13

Expert Comment

by:sonicefu
ID: 20394148
oh sorry !
angelIII ! you are right, i did'nt check the zone from this question is ?
but i am sure it'll work for oralce if ZURINET wants to check it in oracle
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20394162
please check out the below code snippet.

now, I am not 100% sure what you mean with this:
>in Table A, B, C and D ,, taking these tables as variables

can you clarify, please
CREATE PROCEDURE copy_x
AS
  INSERT INTO TABLE_A ( ID, Temp2, Temp3, Temp4, Temp5)
    SELECT ID, TEmp2, Temp3, Temp4, Temp5 
      FROM TABLE_X
 
  INSERT INTO TABLE_B ( ID, Temp2, Temp3, Temp4, Temp5)
    SELECT ID, TEmp2, Temp3, Temp4, Temp5 
      FROM TABLE_X
 
  INSERT INTO TABLE_C ( ID, Temp2, Temp3, Temp4, Temp5)
    SELECT ID, TEmp2, Temp3, Temp4, Temp5 
      FROM TABLE_X
 
  INSERT INTO TABLE_D ( ID, Temp2, Temp3, Temp4, Temp5)
    SELECT ID, TEmp2, Temp3, Temp4, Temp5 
      FROM TABLE_X

Open in new window

0
 

Author Comment

by:ZURINET
ID: 20394180
Hallo Angel..

I need something like @Table                  sysname,

I need to submit the table name during run time and not hard coded..
this concerns the source and destination code.

Thanks for your help
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 20394192
here we go
CREATE PROCEDURE copy_x
  (@source_table sysname
  , @dest_table sysname
  )
AS
 DECLARE @sql nvarchar(1000)
 
  SET @SQL = 'INSERT INTO [' + @dest_table + ']  ( ID, Temp2, Temp3, Temp4, Temp5)
    SELECT ID, TEmp2, Temp3, Temp4, Temp5 
      FROM [' + @source_table + '] '
  
  EXEC (@SQL)

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394218
create proc copytablesimple ( @src sysname, @dest varchar(1000) )
as
declare @smt varchar(4000)
while charindex(',', @dest) > 0 begin
  set @smt = 'insert into ' + left(@dest, charindex(',', @dest)-1) + ' select * from ' + @src
  exec (@smt)
  set @dest = substring(@dest, charindex(',', @dest)+1, 4000)
end
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394224
usage:
exec copytablesimple 'X', 'A,B,C,D'
1. dest tables separated by comma
2. if you need to, you can customize the insert and select column
0
 

Author Comment

by:ZURINET
ID: 20394228
Instead of  SELECT ID, TEmp2, Temp3, Temp4, Temp5

can one write  SELECT *

?
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394230
fix:

Create proc copytablesimple ( @src sysname, @dest varchar(1000) )
as
declare @smt varchar(4000)
while charindex(',', @dest) > 0 begin
  set @smt = 'insert into ' + left(@dest, charindex(',', @dest)-1) + ' select * from ' + @src
  exec (@smt)
  set @dest = substring(@dest, charindex(',', @dest)+1, 4000)
end
if len(@dest) > 1 begin
  set @smt = 'insert into ' + @dest + ' select * from ' + @src
  exec (@smt)
end
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394232
zurinet: have a look at http:#20394230
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20394234
>can one write  SELECT *
if the tables have the same format you described, you should stick away from the select *.
it will give you problems, one day, when the tables are modified without "knowing" of how this procedure works...
0
 

Author Comment

by:ZURINET
ID: 20394248
Will try out "imitchie's code

looks like it will run more faster and economical:-)

0
 

Author Comment

by:ZURINET
ID: 20394280
Hallo Imitchies

Your code have two ends.. but no beginning :-)

Is it possible?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20394288
>Your code have two ends.. but no beginning :-)
I see 2 begin ...   see at the end of the lines " if ... "
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394295
angel got quick eyes

while charindex(',', @dest) > 0 begin
  ....
end
if len(@dest) > 1 begin
  ....
end
0
 

Author Comment

by:ZURINET
ID: 20394310
Thanks :-)

0
 

Author Comment

by:ZURINET
ID: 20394352
Hi imitchie
Error with the solution

Disallowed implicit conversion from data type datetime to data type int, table

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394379
Are the table structures EXACTLY the same?
And what is your EXEC statement exactly, please?
0
 

Author Comment

by:ZURINET
ID: 20394507
Sorry
Just made a copy of two tables..

I am getting this error now

"An explicit value for the identity column in table 'temA' can only be specified when a column list is used and IDENTITY_INSERT is ON."
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394567
Well, that involves testing for whether the destination table has an identity column, and setting IDENTITY INSERT ON which is a lot more complicated.  Not only do you need IDENTITY_INSERT, you also need to know the exact names of the fields that exist in the table.  That script will take at least 2 more hours to build and test and 1 to debug.

What is the purpose of copying into tables that themselves have IDENTITY columns?  If it's not a simple copy/dump operation, then what about INSERT failures?

I'm sorry but I'm stopping at simple copy 1->multiple, no identity columns, no error handling
0
 

Author Comment

by:ZURINET
ID: 20394641
Hoi imitchie

Thanks for your help
I will be taking solution from angelIII
it simple and less complicated..

Thanks.

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394653
how does angel's solution get rid of your IDENTITY_INSERT problem???
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20394663
What a waste of time..
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

695 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