Solved

SQL export, change and import

Posted on 2012-03-27
8
303 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
  • 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
 

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to select a spread of rows in SQL 8 55
datetime in sql 6 29
Update SQL to SP1 on SCCM server 7 13
SQL won't work after disabling SSL3 / TLS1 3 0
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now