Showing posts with label multiple. Show all posts
Showing posts with label multiple. Show all posts

Tuesday, March 20, 2012

Copying rows from multiple tables to a single table

Hi,

I have 3 tables with the follwing schema

Table <Category>

{

UniqueID,

LastDate DateTime

}

Assume the follwing tables with data following the above schema

Table Cat1

{

1, D1

2, D2

3, D3

}

Table Cat2

{

2, D4

3,D5

4, D6

}

Table Cat3

{

1, D7

3,D8

5,D9

}

I have a Master and the schema is as follows

Table master

{

UniqueId,

Cat1 DateTime, -- This is same as the Table name

Cat2 DateTime, -- This is same as the Table name

Cat3 DateTime -- This is same as the Table name

}

After inserting the data from all these 3 tables, I want the my master table to look like this

Table Master

{

UniqueId cat1 cat2 Cat3

- --

1 D1 NULL D7

2 D2 D4 NULL

3 D3 D5 D8

4 NULL D6 NULL

5 NULL NULL D9

}

Please remember the column names will be same as that of table names

can any one pelase let me know the query t o acheive this

Thanks for your quick response

~Mohan Babu

Try:

Code Snippet

insert into table_master([uniqueid], cat1, cat2, cat3)

select

[id],

[Cat1],

[Cat2],

[Cat3]

from

(

select [id], 'Cat1' as table_name, lastdate from dbo.Cat1

union all

select [id], 'Cat2' as table_name, lastdate from dbo.Cat2

union all

select [id], 'Cat3' as table_name, lastdate from dbo.Cat3

) as a

pivot

(

max(lastdate)

for table_name in ([Cat1], [Cat2], [Cat3])

) as pvt

go

AMB

|||

Can you use full outter join style?

CREATE Table #Cat1

(

id int,

val varchar(10)

)

insert into #cat1 (id, val) values (1, 'D1')

insert into #cat1 (id, val) values (2, 'D2')

insert into #cat1 (id, val) values (3, 'D3')

CREATE Table #Cat2

(

id int,

val varchar(10)

)

insert into #cat2 (id, val) values (2, 'D4')

insert into #cat2 (id, val) values (3, 'D5')

insert into #cat2 (id, val) values (4, 'D6')

SELECT

CASE

WHEN C1.Id IS NOT NULL THEN C1.Id

WHEN C2.Id IS NOT NULL THEN C2.Id

ELSE NULL

END AS [Id],

C1.val,

C2.val

FROM #Cat1 c1

FULL OUTER JOIN #cat2 C2 ON

(C1.Id = C2.Id)

DROP TABLE #Cat1

DROP TABLE #Cat2

|||

Code Snippet

Create Table #cat1 (

id int,

[LastDate] datetime

);

Insert Into #cat1 Values('1','01/01/07');

Insert Into #cat1 Values('2','01/02/07');

Insert Into #cat1 Values('3','01/03/07');

Create Table #cat2 (

id int,

[LastDate] datetime

);

Insert Into #cat2 Values('2','01/02/07');

Insert Into #cat2 Values('3','01/03/07');

Insert Into #cat2 Values('4','01/04/07');

SELECT c.id, c.LastDate, d.LastDate FROM (SELECT a.id, b.LastDate FROM (SELECT id FROM #cat1

union

SELECT id FROM #cat2) a LEFT JOIN #cat1 b ON a.id=b.id) c LEFT JOIN #cat2 d ON c.id=d.id

|||

Hi AMB and limon

Thank you bothe very much for your queries.

Both works perfect.

AMB: Can you pelase explain me why u have used Pivt and what is the impact of it in terms of performace and also can u please explain me why you have used max(lastUpdate) and what is the use of it

Limon : Though it works fine i feel the query is bit long.

My concern is that i may have N number of category tables and i have to merge them into a single table.

So i need to build the a query dynamically based on the number of the tables and merge them.

Can any one please explaine me which one executes faster as i might have millions of rows in the category table

Thanks for your time

~Mohan

|||

Dave,

This query inserts null if the ID doesn't exist in botht the tables.

Thanks for your time

~Mohan

|||

Hi mohandbabud,

why did I use pivot?

Because that is what you are doing. You are pivoting rows to columns. When using pivot operator, you need an aggregation function, and It does not make sense to use SUM, AVG, etc, with a [datetime] data type. you could use MIN also. BTW, I supposed that the [id] is unique in each category table.

About which one one will execute faster, I do not know, because both queries are doing heavy stuff. You can tell us when you have finished.

AMB

Copying multiple SPs in SS 2005

I am totally stumped. In SQL Server 2000, I would fire up my EM and right-click on multiple SPs and then Generate Scripts. I then would start QA and run the script on a different DB. This was a very convenient feature to copy SPs over from one DB to other as well as from one machine to other. I can't seem to do the same in SQL Management Studio. Is it possible? If yes, how?


Now this may not be the right place to ask but since VS and SS go hand-in-hand, I thought I would ask.

If you right click on the database -> Tasks -> Generate Scripts you can select the sprocs you want. I know what you mean though, that exact functionality seems to be missing. If you click on procedures and then go to the summary window you can multiple select but can't script from there. Also, you can right click on each sproc and script to clipboard if you want.
Hth,
Scott|||According to Microsoft's SQL Team:

>> Multi-select script didn't get in till recently – its in post-IDW15 builds and will be in the next public CTP of SQL tools.


Monday, March 19, 2012

copying freextex indexes into multiple destination dbs on same dest server

Hi -
I've looked on Microsoft's web site:
http://support.microsoft.com/default...;EN-US;Q240867
and not yet been able to find a full answer to my question.
I'd appreciate any insight from others:
I have a database that has full text indexes on it, that I need to log ship
to multiple other (destination) servers. Also, to promote availability on
each of the destination servers, I plan to restore into two databases:
dest_db and dest_db_alt. Both are to have the same content, but different db
names. I intend to restore one set of dbs/logs from the source server into
standby mode in each destination database, sequentially, so that, with
proper client re-direction, a client hitting the destination server will
always get redirected to a read-only db in standby mode. While one db is
restoring, the other is readable. Ok, fair enough.
Now, the tricky part. The source db is freextex indexed, and one can't
build/rebuild freetext indexes on a db in standby or read-only mode. So,
apparently I'll have to copy the freetext index directory trees from the
source server and restore them on the destination server. What I'm uncertain
about is whether I'll be able to:
a) Use one set of freetext indexes files for both destination dbs (I don't
think so, If I understand things correctly),
b) Properly set the registry keys and directory/file names on the freetext
catalogs on the destination server so that even though at least one of the
destination dbs (necessarily) has a different dbid than the source database,
it will still be able to have a functioning freetext index. From the
generally related material I read it seems as if the registry keys map the
freetext catalog path(s) into SQL Server, and those catalog paths use dbids
and ftcatids integrated into their folder names to map a set of catalog
files to a dbid.
So here is my question: Can I just make multiple copies of the freetext
catalog files, differing only in the dbid portion of the folder name, and
then, using registry keys, map the newly created Freetext catalog folder(s)
into SQL Server with the relevant dbids on the destination server ?
Are dbids, or other database specific unique identifiers, hard coded within
the freetext catalog files themselves ?
Is there other important information I have to consider here ?
Thanks
Steve
Use replication for this. Create your catalogs and full text indexes using a
post snapshot script or create the tables and catalogs in advance, full text
index the tables, and then configure your article to delete the data not
drop and recreate the table (in the article properties section select the
browse button to the right of your table name, and in the snapshot tab, in
the name conflicts section , select delete all data).
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Steve" <Steve_a013452@.newsgroups.nospam> wrote in message
news:OJ9F4IqGGHA.2704@.TK2MSFTNGP15.phx.gbl...
> Hi -
> I've looked on Microsoft's web site:
> http://support.microsoft.com/default...;EN-US;Q240867
> and not yet been able to find a full answer to my question.
> I'd appreciate any insight from others:
> I have a database that has full text indexes on it, that I need to log
> ship to multiple other (destination) servers. Also, to promote
> availability on each of the destination servers, I plan to restore into
> two databases: dest_db and dest_db_alt. Both are to have the same content,
> but different db names. I intend to restore one set of dbs/logs from the
> source server into standby mode in each destination database,
> sequentially, so that, with proper client re-direction, a client hitting
> the destination server will always get redirected to a read-only db in
> standby mode. While one db is restoring, the other is readable. Ok, fair
> enough.
> Now, the tricky part. The source db is freextex indexed, and one can't
> build/rebuild freetext indexes on a db in standby or read-only mode. So,
> apparently I'll have to copy the freetext index directory trees from the
> source server and restore them on the destination server. What I'm
> uncertain about is whether I'll be able to:
> a) Use one set of freetext indexes files for both destination dbs (I don't
> think so, If I understand things correctly),
> b) Properly set the registry keys and directory/file names on the freetext
> catalogs on the destination server so that even though at least one of the
> destination dbs (necessarily) has a different dbid than the source
> database, it will still be able to have a functioning freetext index. From
> the generally related material I read it seems as if the registry keys map
> the freetext catalog path(s) into SQL Server, and those catalog paths use
> dbids and ftcatids integrated into their folder names to map a set of
> catalog files to a dbid.
> So here is my question: Can I just make multiple copies of the freetext
> catalog files, differing only in the dbid portion of the folder name, and
> then, using registry keys, map the newly created Freetext catalog
> folder(s) into SQL Server with the relevant dbids on the destination
> server ?
> Are dbids, or other database specific unique identifiers, hard coded
> within the freetext catalog files themselves ?
> Is there other important information I have to consider here ?
> Thanks
> Steve
>

Thursday, March 8, 2012

Copying data from multiple tables to one table

Hi,

I have 3 tables with the follwing schema

Table <Category>

{

UniqueID,

LastDate DateTime

}

Assume the follwing tables with data following the above schema

Table Cat1

{

1, D1

2, D2

3, D3

}

Table Cat2

{

2, D4

3,D5

4, D6

}

Table Cat3

{

1, D7

3,D8

5,D9

}

I have a Master and the schema is as follows

Table master

{

UniqueId,

Cat1 DateTime, -- This is same as the Table name

Cat2 DateTime, -- This is same as the Table name

Cat3 DateTime -- This is same as the Table name

}

After inserting the data from all these 3 tables, I want the my master table to look like this

Table Master

{

UniqueId cat1 cat2 Cat3

- --

1 D1 NULL D7

2 D2 D4 NULL

3 D3 D5 D8

4 NULL D6 NULL

5 NULL NULL D9

}

Please remember the column names will be same as that of table names

can any one pelase let me know the query t o acheive this

Thanks for your quick response

~Mohan Babu

Follow these steps

(a) Insert first table to master ie Cat1 to Master

Insert into TableMaster (UniqueID,Cat1) Select Col1,Col2 from Cat1

(b) Insert into master table from Cat2 only those uniqueid which are not in master

Insert into TableMaster (UniqueID,Cat2) Select Col1,Col2 from Cat2 Where Col1 not In (Select UniqueID from TableMaster )

(c) Update Cat2 column of master table

Update TableMaster Set Cat2=A.Col2 from Cat2 A

inner join TableMaster B on b.UniqueID=a.Col1

(d) Insert into master from Cat3

Insert into TableMaster (UniqueID,Cat3) Select Col1,Col2 from Cat3 Where Col1 not In (Select UniqueID from TableMaster )

(e) Update TableMaster Set Cat3=A.Col2 from Cat3 A

inner join TableMaster B on b.UniqueID=a.Col1

Madhu

Monday, February 13, 2012

Copy rows from multiple tables into one table (in another database)

My boss has been using linked tables (read only) from Access to copy data
from our accounting system to his pc. Instead of using Access I would like
to copy rows from different tables into a new table in a different database.
I want to use a different database for security reasons because the new
database will be accessed by the web server.
What is the best way to simulate Access' linked tables and can the
resolution be realtime or would I need to run a query once a day to have the
most current info in my new table?You can either use DTS or Replication depends on how your boss want to see
the data as exactly current or could be sometime delay.
Perayu
"JohnS" <JohnSPAM@.hotmail.com> wrote in message
news:e4d43AfvFHA.3256@.TK2MSFTNGP09.phx.gbl...
> My boss has been using linked tables (read only) from Access to copy data
> from our accounting system to his pc. Instead of using Access I would
> like to copy rows from different tables into a new table in a different
> database. I want to use a different database for security reasons because
> the new database will be accessed by the web server.
> What is the best way to simulate Access' linked tables and can the
> resolution be realtime or would I need to run a query once a day to have
> the most current info in my new table?
>
>|||It looks like I should use SQL triggers to update information in new table
when changes have been made in the accounting database. What is the best
way to export data rows from different tables to one new table and does
anyone have an example of how to create a trigger that watches many tables,
but only inserts into one table?
"JohnS" <JohnSPAM@.hotmail.com> wrote in message
news:e4d43AfvFHA.3256@.TK2MSFTNGP09.phx.gbl...
> My boss has been using linked tables (read only) from Access to copy data
> from our accounting system to his pc. Instead of using Access I would
> like to copy rows from different tables into a new table in a different
> database. I want to use a different database for security reasons because
> the new database will be accessed by the web server.
> What is the best way to simulate Access' linked tables and can the
> resolution be realtime or would I need to run a query once a day to have
> the most current info in my new table?
>
>|||Sometime delayed is fine with us. I'll research those two topics, but which
is less complicated seeing as I'm not a SQL guru.
"JohnS" <JohnSPAM@.hotmail.com> wrote in message
news:O%23lvOifvFHA.3100@.TK2MSFTNGP12.phx.gbl...
> It looks like I should use SQL triggers to update information in new table
> when changes have been made in the accounting database. What is the best
> way to export data rows from different tables to one new table and does
> anyone have an example of how to create a trigger that watches many
> tables, but only inserts into one table?
> "JohnS" <JohnSPAM@.hotmail.com> wrote in message
> news:e4d43AfvFHA.3256@.TK2MSFTNGP09.phx.gbl...
>|||In my opinion, DTS is a easier one. Sometime, Replication is hard to
maintain whenever you try to change the related data structure.
Perayu
"JohnS" <JohnSPAM@.hotmail.com> wrote in message
news:Oi4bWnfvFHA.2132@.TK2MSFTNGP15.phx.gbl...
> Sometime delayed is fine with us. I'll research those two topics, but
> which is less complicated seeing as I'm not a SQL guru.
> "JohnS" <JohnSPAM@.hotmail.com> wrote in message
> news:O%23lvOifvFHA.3100@.TK2MSFTNGP12.phx.gbl...
>|||The functional equivalent to Access's linked tables is to set up a Linked
Server in SQL Server. A linked server can be any OLEDB data source. Any
tables in the remote system would be available through this technique.
"JohnS" <JohnSPAM@.hotmail.com> wrote in message
news:e4d43AfvFHA.3256@.TK2MSFTNGP09.phx.gbl...
> My boss has been using linked tables (read only) from Access to copy data
> from our accounting system to his pc. Instead of using Access I would
> like to copy rows from different tables into a new table in a different
> database. I want to use a different database for security reasons because
> the new database will be accessed by the web server.
> What is the best way to simulate Access' linked tables and can the
> resolution be realtime or would I need to run a query once a day to have
> the most current info in my new table?
>
>|||I tried DTS but didn't find anyway to choose which rows in a table I want.
It seemed to be the whole table or nothing at all. The other problem was
data from the accounting system would wipe out the target destination data
and I want them to merge.
Can I choose certain rows from different tables and merge them into one
table with Linked Server?
"Jeremy Williams" <jeremydwill@.netscape.net> wrote in message
news:uFybfFhvFHA.2504@.TK2MSFTNGP15.phx.gbl...
> The functional equivalent to Access's linked tables is to set up a Linked
> Server in SQL Server. A linked server can be any OLEDB data source. Any
> tables in the remote system would be available through this technique.
> "JohnS" <JohnSPAM@.hotmail.com> wrote in message
> news:e4d43AfvFHA.3256@.TK2MSFTNGP09.phx.gbl...
>|||Well, DTS and linked servers are kind of "apples and oranges" topics.
In DTS, you can use regular SQL statements to define which columns and rows
you want to retrieve from your source connection, and you can define where
that data gets placed in your destination connection. DTS is a fairly large
topic with many sub-topics. I would encourage you to thouroughly review the
topic in SQL Server Books Online (BOL).
As for linked servers, all I was pointing out was that you can set up a
linked server in SQL Server in a somewhat similar fashion to the way you set
up linked tables in MS Access. Again, this is a moderately involved topic.
For more information, see the topic "Accessing External Data" in BOL.
"JohnS" <JohnSPAM@.hotmail.com> wrote in message
news:eXBFQPivFHA.904@.tk2msftngp13.phx.gbl...
>I tried DTS but didn't find anyway to choose which rows in a table I want.
>It seemed to be the whole table or nothing at all. The other problem was
>data from the accounting system would wipe out the target destination data
>and I want them to merge.
> Can I choose certain rows from different tables and merge them into one
> table with Linked Server?
>
> "Jeremy Williams" <jeremydwill@.netscape.net> wrote in message
> news:uFybfFhvFHA.2504@.TK2MSFTNGP15.phx.gbl...
>