June 18, 2015 at 8:25 am
I have two table in two different database. I have to found count for both table and put into other table.
example : Database --> A , table is T1
Database --> B , table is T2
I want count of both table and put into T3 table in database A or B .
Please let me know how to do this.
June 18, 2015 at 8:32 am
rajeshjaiswalraj (6/18/2015)
I have two table in two different database. I have to found count for both table and put into other table.example : Database --> A , table is T1
Database --> B , table is T2
I want count of both table and put into T3 table in database A or B .
Please let me know how to do this.
https://msdn.microsoft.com/en-us/library/ms177563.aspx
Assuming databases on the same server, query using three part naming convention; "[database].[schema].[object]".
June 18, 2015 at 8:45 am
Yes ............ Database is in same server. Please help me on this
June 18, 2015 at 9:20 am
rajeshjaiswalraj (6/18/2015)
Yes ............ Database is in same server. Please help me on this
As Cadavre already said...you three part naming. [database].[schema].[object]
What part(s) do you not understand of what you need to do?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 18, 2015 at 9:32 am
rajeshjaiswalraj (6/18/2015)
Yes ............ Database is in same server. Please help me on this
Show us the code you have written so far using the advice you've been given. Then we can give you more guidance on getting the results you need.
-SQLBill
June 18, 2015 at 1:55 pm
Try using this...
DECLARE @totalcnt int
DECLARE @cnt1 int
DECLARE @cnt2 INT
SET @cnt1 = (SELECT COUNT(*) FROM [database].[schema].table1)
SET @cnt2 = (SELECT COUNT(*) FROM [database].[schema].table2)
SET @totalcnt = @cnt1 + @cnt2
INSERT INTO [database].[schema].table3
SELECT @totalcnt
June 18, 2015 at 1:58 pm
SolveSQL (6/18/2015)
Try using this...DECLARE @totalcnt int
DECLARE @cnt1 int
DECLARE @cnt2 INT
SET @cnt1 = (SELECT COUNT(*) FROM table1)
SET @cnt2 = (SELECT COUNT(*) FROM table2)
SET @totalcnt = @cnt1 + @cnt2
INSERT INTO table3
SELECT @totalcnt
You could certainly simplify this significantly if this is what the OP wants.
Insert table3
SELECT (SELECT COUNT(*) FROM table1) + (SELECT COUNT(*) FROM table2)
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 18, 2015 at 2:00 pm
seems like that's what is asked.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply