Showing posts with label db1. Show all posts
Showing posts with label db1. Show all posts

Friday, March 9, 2012

Need advice for process of swapping DBs

I need to build a *.sql script that will remove a database (let's call
it "DB1") and replace it with a brand new empty database (let's call it
"DB2").

Caveat: I don't want to be left with database "DB1" having it's files
confusingly named "DB2.mdf" and "DB2_log.ldf". These two files should
also be renamed to "DB1.mdf" and "DB1_log.ldf" so that outside
customers are not left confused. In addition, I need to be able to
restore the original DB1 if anything goes wrong during, or even after,
the entire process.

Let's assume every customer's *.mdf's and *.ldf's will always reside in
C:\Program Files\Microsoft SQL Server\MSSQL\data folder.

I've researched sp_attach_db, but this looks more appropriate for
moving databases. This isn't what I want to do.

Thank you in advance.Simple.. In EM you can right right the database and "All tasks -
Generate SQL Script".. Go through and choose want you want to script
i.e. tables, views, stored procs, constraints, create database etc
etc.. Save the script as a .sql on your computer.. Open the file in a
text editor or SQL QA - modify the create database command and add a
"drop database" command at the top.

Wednesday, March 7, 2012

Need a query to compare tables

I have two identical tables on two DBs, DB1.Table1 and DB2.table2. DB1.Table1 has more data than DB2.Table1. I need to find out what data in DB1.Table1 is not in DB2.Table1, but I can't do it by the ROWGUID column because not all ROWGUID values were carried over the last time the DBs were split, so identical rows may not have the same rowguid value. And I can't use the PK column because that's the Identity column which is not unique between these two tables.
What I need to do is compare the combined columns in one table (not including the Identity and rowguid columns, of course) with the same columns in the other table.
Queries are not my strong point. Does anyone know how I can do this? I'm desperate!
Thanks in advance.
JohnJohn,
Assuming that there is another unique index in addition to the PK and
ROWGUID columns, use that to manage the joins. (If there is no other unique
identifier than the two that are not reliable, then the problem becomes much
harder.) Something like:
SELECT * FROM DB1.Table1
WHERE UniqueString + CONVERT(VARCHAR(10),UniqueInt)
NOT IN
(SELECT UniqueString + CONVERT(VARCHAR(10), UniqueInt)
FROM DB2.Table1)
Russell Fields
"John Steen" <anonymous@.discussions.microsoft.com> wrote in message
news:94B4A891-E2B2-4F43-8059-E34BCD82B5C7@.microsoft.com...
> I have two identical tables on two DBs, DB1.Table1 and DB2.table2.
DB1.Table1 has more data than DB2.Table1. I need to find out what data in
DB1.Table1 is not in DB2.Table1, but I can't do it by the ROWGUID column
because not all ROWGUID values were carried over the last time the DBs were
split, so identical rows may not have the same rowguid value. And I can't
use the PK column because that's the Identity column which is not unique
between these two tables.
> What I need to do is compare the combined columns in one table (not
including the Identity and rowguid columns, of course) with the same columns
in the other table.
> Queries are not my strong point. Does anyone know how I can do this? I'm
desperate!
> Thanks in advance.
> John|||Thanks for the quick reply, Russell. Unfortunately, there's not another unique index on this table other than the ones on the Identity and ROWGUID columns. Is there a way to compare the combined values of the rest of the columns? If not, then I'll have to use the ROWGUID column and hope duplicate records aren't created.
Thanks,
John
-- Russell Fields wrote: --
John,
Assuming that there is another unique index in addition to the PK and
ROWGUID columns, use that to manage the joins. (If there is no other unique
identifier than the two that are not reliable, then the problem becomes much
harder.) Something like:
SELECT * FROM DB1.Table1
WHERE UniqueString + CONVERT(VARCHAR(10),UniqueInt)
NOT IN
(SELECT UniqueString + CONVERT(VARCHAR(10), UniqueInt)
FROM DB2.Table1)
Russell Fields
"John Steen" <anonymous@.discussions.microsoft.com> wrote in message
news:94B4A891-E2B2-4F43-8059-E34BCD82B5C7@.microsoft.com...
> I have two identical tables on two DBs, DB1.Table1 and DB2.table2.
DB1.Table1 has more data than DB2.Table1. I need to find out what data in
DB1.Table1 is not in DB2.Table1, but I can't do it by the ROWGUID column
because not all ROWGUID values were carried over the last time the DBs were
split, so identical rows may not have the same rowguid value. And I can't
use the PK column because that's the Identity column which is not unique
between these two tables.
>> What I need to do is compare the combined columns in one table (not
including the Identity and rowguid columns, of course) with the same columns
in the other table.
>> Queries are not my strong point. Does anyone know how I can do this? I'm
desperate!
>> Thanks in advance.
>> John|||John,
probably no other method than the clumsy way of comparing the whole record
in both table:
set concat_null_yields_null off
select t1.PK
from t1
left outer join t2
on t1.c1+t1.c2+t1.c3+... = t2.c1+t2.c2+t2.c3+...
where t2.PK is null
yields the records in t1 but not in t2, and
select t2.PK
from t2
left outer join t1
on t1.c1+t1.c2+t1.c3+... = t2.c1+t2.c2+t2.c3+...
where t1.PK is null
yields the records in t2 but not in t1. There is a checksum function that
may or may not help the processing. If your table is large, you may need to
cycle through the table.
hth
Quentin
"John Steen" <anonymous@.discussions.microsoft.com> wrote in message
news:3937945E-493B-462B-A57E-51C7818B23FD@.microsoft.com...
> Thanks for the quick reply, Russell. Unfortunately, there's not another
unique index on this table other than the ones on the Identity and ROWGUID
columns. Is there a way to compare the combined values of the rest of the
columns? If not, then I'll have to use the ROWGUID column and hope
duplicate records aren't created.
> Thanks,
> John
> -- Russell Fields wrote: --
> John,
> Assuming that there is another unique index in addition to the PK and
> ROWGUID columns, use that to manage the joins. (If there is no other
unique
> identifier than the two that are not reliable, then the problem
becomes much
> harder.) Something like:
> SELECT * FROM DB1.Table1
> WHERE UniqueString + CONVERT(VARCHAR(10),UniqueInt)
> NOT IN
> (SELECT UniqueString + CONVERT(VARCHAR(10), UniqueInt)
> FROM DB2.Table1)
> Russell Fields
> "John Steen" <anonymous@.discussions.microsoft.com> wrote in message
> news:94B4A891-E2B2-4F43-8059-E34BCD82B5C7@.microsoft.com...
> > I have two identical tables on two DBs, DB1.Table1 and DB2.table2.
> DB1.Table1 has more data than DB2.Table1. I need to find out what
data in
> DB1.Table1 is not in DB2.Table1, but I can't do it by the ROWGUID
column
> because not all ROWGUID values were carried over the last time the
DBs were
> split, so identical rows may not have the same rowguid value. And I
can't
> use the PK column because that's the Identity column which is not
unique
> between these two tables.
> >> What I need to do is compare the combined columns in one table
(not
> including the Identity and rowguid columns, of course) with the same
columns
> in the other table.
> >> Queries are not my strong point. Does anyone know how I can do
this? I'm
> desperate!
> >> Thanks in advance.
> >> John
>
>