Solved

SQL export, change and import

Posted on 2012-03-27
8
313 Views
Last Modified: 2012-03-28
Hi

I want to export all my tables/tablerows with DATASET-column = 'TST'.
And then IMPORT the tables again (APPEND), with the DATASET-Column set to '123'.

Any ideas .. ?
0
Comment
Question by:conceptdata
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Expert Comment

by:tsnirone
ID: 37770747
Update?
0
 
LVL 3

Expert Comment

by:tsnirone
ID: 37770749
insert into yourtable select col1, col2, col3, "123" from yourtable

?
0
 

Author Comment

by:conceptdata
ID: 37770756
Not update, the 'tst' DATAset rows still needed.
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.

 

Author Comment

by:conceptdata
ID: 37770761
I think the thing i'm looking for is a script the exports .
And then a script that imports.
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 37770802
So if you start with a table dbo.aa with column DataSet you end up with all the records where DataSet originally equals 'TST' containing DataSet equal to '123' ?

For one table, this is:

update dbo.aa
    set DataSet = '123'
    where DataSet = 'TST'

Then you have to repeat this for every table that has a field DataSet in it?

The code might be put into a stored procedure:

create procedure dbo.ChangeDataSet
(  @strTableName  varchar(64)    )
as
Begin
   declare @strCmd varchar(2000)
   set @strCmd = 'update ' + @strTableName + '  '
   set @strCmd = @cmd + '  set DataSet = ''123''    '
   set @strCmd = @cmd + '  where DataSet = ''TST''   '
   exec(@strCmd)
End

which gets called for each table like this:

exec dbo.ChangeDataSet(@strTableName)

And the code to get a list of all the tables with a column called DataSet:

declare @taTableList table (TableName varchar(64)
insert into @taTableList
SELECT table_name=sysobjects.name
    FROM sysobjects
    JOIN syscolumns ON sysobjects.id = syscolumns.id
   WHERE sysobjects.xtype='U'
      and syscolumns.name = 'DataSet'

select * from @taTableList

Then you can either use a cursor or a loop to work through @taTableList, getting each table name in turn and running the stored procedure on it.
0
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 37770817
And now you've said "Not update, the 'tst' DATAset rows still needed."

You could adapt the code above by changing the inside of the procedure :
copy  * into tmp from TableName where DataSet = 'TST'
update tmp set DataSet = '123'
insert into TableName select * from tmp
drop table tmp

but you'll have to be aware of uniqueness constraints and identity fields in the table you're putting the data back into!
0
 
LVL 3

Expert Comment

by:tsnirone
ID: 37770829
If you effectively want to duplicate the rows in your table with the new ones having '123' instead of 'TST' the sql I posted should work

insert into yourtable select col1, col2, col3, "123" from yourtable

Be aware though as DcpKing said about uniqueness.....
0
 

Author Comment

by:conceptdata
ID: 37770831
Thanks, I'm looking in your suggested solutions later today :)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

628 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