Solved

SQL export, change and import

Posted on 2012-03-27
8
311 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Results to Excel File 18 76
SQL Job Hung 17 37
SQL Pivot table 2 43
Find unused columns in a table 12 70
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

756 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