Solved

SQL export, change and import

Posted on 2012-03-27
8
308 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find SQL query used by application 3 20
TSQL query to generate xml 4 36
How to resolve SQL Server DB deadlock which makes my application hangs ? 6 32
Help in Bulk Insert 9 35
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

832 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