Solved

SQL export, change and import

Posted on 2012-03-27
8
301 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 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

19 Experts available now in Live!

Get 1:1 Help Now