Friday, March 30, 2012
Query across multiple db's
was moving the data and out of 370K records, 24 did not come across. How do I
query to find out which records did not make it across.
Hi,
select * from DB1..tablename where id not in(select id from DB2..tablename)
Thanks
Hari
MCDBA
"DBA" <DBA@.discussions.microsoft.com> wrote in message
news:12D870F3-4DE5-44CA-89BC-9BD387517EFC@.microsoft.com...
> I have two databases on one server. I want do a query across the
databases. I
> was moving the data and out of 370K records, 24 did not come across. How
do I
> query to find out which records did not make it across.
|||Did not work. I think because the PK is across 5 fields
"Hari Prasad" wrote:
> Hi,
> select * from DB1..tablename where id not in(select id from DB2..tablename)
>
> Thanks
> Hari
> MCDBA
>
> "DBA" <DBA@.discussions.microsoft.com> wrote in message
> news:12D870F3-4DE5-44CA-89BC-9BD387517EFC@.microsoft.com...
> databases. I
> do I
>
>
|||One way to finese this problem is:
SELECT * FROM DB1..tablename
WHERE CONVERT(CHAR(10), IDCol1) +
CONVERT(CHAR(10), IDCol2) +
CONVERT(CHAR(10), IDCol3) +
CONVERT(CHAR(10), IDCol4) +
CONVERT(CHAR(10), IDCol5)
NOT IN
(SELECT
CONVERT(CHAR(10), IDCol1) +
CONVERT(CHAR(10), IDCol2) +
CONVERT(CHAR(10), IDCol3) +
CONVERT(CHAR(10), IDCol4) +
CONVERT(CHAR(10), IDCol5)
FROM DB2..tablename)
That assumes that CHAR(10) will hold the conversion of your identifying
columns. Adjust appropriately.
Russell Fields
"DBA" <DBA@.discussions.microsoft.com> wrote in message
news:8655C387-155D-4AE6-BFAF-7DB3DFD50593@.microsoft.com...[vbcol=seagreen]
> Did not work. I think because the PK is across 5 fields
> "Hari Prasad" wrote:
DB2..tablename)[vbcol=seagreen]
How[vbcol=seagreen]
|||Or you could use NOT EXISTS, which in my opinion reads a bit better:
SELECT *
FROM DB1..tablename T1
WHERE NOT EXISTS
(SELECT *
FROM DB2..tablename T2
WHERE T2.IDCol1 = T1.IDCol1
AND T2.IDCol2 = T2.IDCol2
AND T2.IDCol3 = T2.IDCol3
AND T2.IDCol4 = T2.IDCol4
AND T2.IDCol5 = T2.IDCol5)
"Russell Fields" <RussellFields@.NoMailPlease.Com> wrote in message
news:%23hli%23S7hEHA.3536@.TK2MSFTNGP12.phx.gbl...
> One way to finese this problem is:
> SELECT * FROM DB1..tablename
> WHERE CONVERT(CHAR(10), IDCol1) +
> CONVERT(CHAR(10), IDCol2) +
> CONVERT(CHAR(10), IDCol3) +
> CONVERT(CHAR(10), IDCol4) +
> CONVERT(CHAR(10), IDCol5)
> NOT IN
> (SELECT
> CONVERT(CHAR(10), IDCol1) +
> CONVERT(CHAR(10), IDCol2) +
> CONVERT(CHAR(10), IDCol3) +
> CONVERT(CHAR(10), IDCol4) +
> CONVERT(CHAR(10), IDCol5)
> FROM DB2..tablename)
> That assumes that CHAR(10) will hold the conversion of your identifying
> columns. Adjust appropriately.
> Russell Fields
Query across multiple db's
I
was moving the data and out of 370K records, 24 did not come across. How do
I
query to find out which records did not make it across.Hi,
select * from DB1..tablename where id not in(select id from DB2..tablename)
Thanks
Hari
MCDBA
"DBA" <DBA@.discussions.microsoft.com> wrote in message
news:12D870F3-4DE5-44CA-89BC-9BD387517EFC@.microsoft.com...
> I have two databases on one server. I want do a query across the
databases. I
> was moving the data and out of 370K records, 24 did not come across. How
do I
> query to find out which records did not make it across.|||Did not work. I think because the PK is across 5 fields
"Hari Prasad" wrote:
> Hi,
> select * from DB1..tablename where id not in(select id from DB2..tablename
)
>
> Thanks
> Hari
> MCDBA
>
> "DBA" <DBA@.discussions.microsoft.com> wrote in message
> news:12D870F3-4DE5-44CA-89BC-9BD387517EFC@.microsoft.com...
> databases. I
> do I
>
>|||One way to finese this problem is:
SELECT * FROM DB1..tablename
WHERE CONVERT(CHAR(10), IDCol1) +
CONVERT(CHAR(10), IDCol2) +
CONVERT(CHAR(10), IDCol3) +
CONVERT(CHAR(10), IDCol4) +
CONVERT(CHAR(10), IDCol5)
NOT IN
(SELECT
CONVERT(CHAR(10), IDCol1) +
CONVERT(CHAR(10), IDCol2) +
CONVERT(CHAR(10), IDCol3) +
CONVERT(CHAR(10), IDCol4) +
CONVERT(CHAR(10), IDCol5)
FROM DB2..tablename)
That assumes that CHAR(10) will hold the conversion of your identifying
columns. Adjust appropriately.
Russell Fields
"DBA" <DBA@.discussions.microsoft.com> wrote in message
news:8655C387-155D-4AE6-BFAF-7DB3DFD50593@.microsoft.com...[vbcol=seagreen]
> Did not work. I think because the PK is across 5 fields
> "Hari Prasad" wrote:
>
DB2..tablename)[vbcol=seagreen]
How[vbcol=seagreen]|||Or you could use NOT EXISTS, which in my opinion reads a bit better:
SELECT *
FROM DB1..tablename T1
WHERE NOT EXISTS
(SELECT *
FROM DB2..tablename T2
WHERE T2.IDCol1 = T1.IDCol1
AND T2.IDCol2 = T2.IDCol2
AND T2.IDCol3 = T2.IDCol3
AND T2.IDCol4 = T2.IDCol4
AND T2.IDCol5 = T2.IDCol5)
"Russell Fields" <RussellFields@.NoMailPlease.Com> wrote in message
news:%23hli%23S7hEHA.3536@.TK2MSFTNGP12.phx.gbl...
> One way to finese this problem is:
> SELECT * FROM DB1..tablename
> WHERE CONVERT(CHAR(10), IDCol1) +
> CONVERT(CHAR(10), IDCol2) +
> CONVERT(CHAR(10), IDCol3) +
> CONVERT(CHAR(10), IDCol4) +
> CONVERT(CHAR(10), IDCol5)
> NOT IN
> (SELECT
> CONVERT(CHAR(10), IDCol1) +
> CONVERT(CHAR(10), IDCol2) +
> CONVERT(CHAR(10), IDCol3) +
> CONVERT(CHAR(10), IDCol4) +
> CONVERT(CHAR(10), IDCol5)
> FROM DB2..tablename)
> That assumes that CHAR(10) will hold the conversion of your identifying
> columns. Adjust appropriately.
> Russell Fields
Wednesday, March 28, 2012
query using the record number
my table has 200,000 + records and I want to see the particular record which I think it is causing the problem.
How could i query 195,203rd record?
thank you, yanorooWhat problem is it causing? How did you figure out the number to begin with?|||What problem is it causing? How did you figure out the number to begin with?
oh, the record number is just an example.
actual case is -I ran dts package to import data from txt file to sql table.
The dts truncated the table and imported the records correctly (218503 record) but I can't find the data from 208775th record on the table...
I want to see the what was populated into the table from 208775th record.
And strange thing is the table has 237043 record (which is more than input)|||are you getting 237043 by doing a count(*) on the table?|||are you getting 237043 by doing a count(*) on the table?
yes, it is by count(*) on the table.|||If you are just debugging the process, then temporarily create an incrementing identity column on the table.|||If you are just debugging the process, then temporarily create an incrementing identity column on the table.
Hello,
Found out the problem was DTS prod installation error.
Thank you anyway for your suggestion.
Yanoroo|||i suggest in a situation like this to create a staging table when you transfer large amounts of data to an sql server. this way you can set the error count high and allow sql server to leave the rows causing the error in the staging table while you move the clean data to the final destination. you can then have the dts send you an email notifying you of the prob and you can clean those rows in the morning.
go to www.SQLDts.com|||I assume there is an unique field named pid in your table and you are looking for a particular record of pid(in this case 195,203rd).You can try this query--
select * from mytable where pid not in
(select top 195,202 pid from mytable)
If you have any other type of requirement that can be solved too.
Subhasish
Query - concatenating across related records
Table A
--
ID........Code
A............X
B............X
C............Y
Table B
--
Code......Desc_Line......Desc
X.............01.........This is a description
X.............02.........for Code X
Y.............01.........Code Y is one line
I'd like the query to return:
A...X...This is a description for Code X
B...X...This is a description for Code X
C...Y...Code Y is one line
Question: How can I concatenate the multiple description lines in table B
to display on a single line with the corresponding data from Table A? I'd
like something easily re-usable (e.g. User Defined Function), but I'm not
sure how to go about it. Any help/suggestions would be greatly appreciated.SELECT A.id, B.code,
MAX(CASE WHEN desc_line = '01' THEN [desc] ELSE '' END)+
MAX(CASE WHEN desc_line = '02' THEN [desc] ELSE ' ' END)+
MAX(CASE WHEN desc_line = '03' THEN [desc] ELSE ' ' END)+
..
FROM TableA AS A
JOIN TableB AS B
ON A.code = B.code
GROUP BY A.id, B.code
"desc" is a reserved word. Avoid using it as a column name.
David Portas
SQL Server MVP
--|||I change the field Desc to Descrip, since Desc is a key word (Abbreviation
for Descending)
Create Function dbo.GetDescription (@.Code Char(1))
Returns VarChar(300)
As
Begin
Declare @.Out VarChar(300) Set @.Out = ''
Declare @.Lin Integer Set @.Lin = 0
While Exists (Select * From TableB
Where Code = @.Code
And Desc_Line > @.Lin)
Begin
Select @.Lin = Min(Desc_Line)
From TableB
Where Code = @.Code
And Desc_Line > @.Lin
-- --
Select @.Out = @.Out + Descrip + ' '
From TB
Where Code = @.Code
And Desc_Line = @.Lin
End
Return RTrim(@.Out)
End
-- ----
You use it like this:
dbo.GetDescription('X')
as in:
Select dbo.GetDescription('X')
"bradm98" wrote:
> Here's the setup:
> Table A
> --
> ID........Code
> A............X
> B............X
> C............Y
> Table B
> --
> Code......Desc_Line......Desc
> X.............01.........This is a description
> X.............02.........for Code X
> Y.............01.........Code Y is one line
> I'd like the query to return:
> A...X...This is a description for Code X
> B...X...This is a description for Code X
> C...Y...Code Y is one line
>
> Question: How can I concatenate the multiple description lines in table B
> to display on a single line with the corresponding data from Table A? I'd
> like something easily re-usable (e.g. User Defined Function), but I'm not
> sure how to go about it. Any help/suggestions would be greatly appreciated.[/colo
r]|||OH, I missed one TB -> TableB ... Here's correctted...
Create Function dbo.GetDescription (@.Code Char(1))
Returns VarChar(300)
As
Begin
Declare @.Out VarChar(300) Set @.Out = ''
Declare @.Lin Integer Set @.Lin = 0
While Exists (Select * From TableB
Where Code = @.Code
And Desc_Line > @.Lin)
Begin
Select @.Lin = Min(Desc_Line)
From TableB
Where Code = @.Code
And Desc_Line > @.Lin
-- --
Select @.Out = @.Out + Descrip + ' '
From TableB
Where Code = @.Code
And Desc_Line = @.Lin
End
Return RTrim(@.Out)
End
"CBretana" wrote:
> I change the field Desc to Descrip, since Desc is a key word (Abbreviation
> for Descending)
> Create Function dbo.GetDescription (@.Code Char(1))
> Returns VarChar(300)
> As
> Begin
> Declare @.Out VarChar(300) Set @.Out = ''
> Declare @.Lin Integer Set @.Lin = 0
> While Exists (Select * From TableB
> Where Code = @.Code
> And Desc_Line > @.Lin)
> Begin
> Select @.Lin = Min(Desc_Line)
> From TableB
> Where Code = @.Code
> And Desc_Line > @.Lin
> -- --
> Select @.Out = @.Out + Descrip + ' '
> From TB
> Where Code = @.Code
> And Desc_Line = @.Lin
> End
> Return RTrim(@.Out)
> End
> -- ----
> You use it like this:
> dbo.GetDescription('X')
> as in:
> Select dbo.GetDescription('X')
>
> "bradm98" wrote:
>|||Thanks so much. David's query worked, but your function really hit the nail
on the head!
"CBretana" wrote:
> OH, I missed one TB -> TableB ... Here's correctted...
> Create Function dbo.GetDescription (@.Code Char(1))
> Returns VarChar(300)
> As
> Begin
> Declare @.Out VarChar(300) Set @.Out = ''
> Declare @.Lin Integer Set @.Lin = 0
> While Exists (Select * From TableB
> Where Code = @.Code
> And Desc_Line > @.Lin)
> Begin
> Select @.Lin = Min(Desc_Line)
> From TableB
> Where Code = @.Code
> And Desc_Line > @.Lin
> -- --
> Select @.Out = @.Out + Descrip + ' '
> From TableB
> Where Code = @.Code
> And Desc_Line = @.Lin
> End
> Return RTrim(@.Out)
> End
>
> "CBretana" wrote:
>|||Just be aware, That using UDFs which read data from database tables is
frowned upon because in SQL 7/ SQL2k, every time the UDF Runs, it is
recompiled... So if, or example, you run
Select ID, Code, dbo.GetDescription(Code)
From TableA
And TableA has a million rows, then you will be recompiling the UDF a
million times. It can be a real performance hog...
I'm hoping MS fixes this in SQL 2005...
UDFs are at their best when they do NOT access data from database tables
"bradm98" wrote:
> Thanks so much. David's query worked, but your function really hit the na
il
> on the head!
> "CBretana" wrote:
>sql
Monday, March 26, 2012
Query
OrderNum, I would like to copy 1st record for each
OrderNum from this table into a new table and then update
the OrderNum record in the 2nd table with values from
rest of the records in the 1st table, can someone help me
in doing this?
Ex.
Frist table may have -
ORder1 AreaField1 description ...
ORder1 AreaField1 Description ...
Order1 AreaField2 Description ...
ORder2 AreadField Description ...
Order2 AreaField Description ...
ORder3 AreaField Description ...
ORder4 AreaField Description ...
I would like the 2nd table to look lik -
ORder1 AreaField1+AreaField2 Description ...
ORder2 AreaField Description ...
Order3 AreaField Description ...
ORder4 AreaField Description...
Thank you very much,
-Kim
Xref: TK2MSFTNGP08.phx.gbl microsoft.public.sqlserver.mseq:7748
On Thu, 12 Aug 2004 11:56:32 -0700, Kim wrote:
(snip)
Hi Kim,
I just replied to an equal post in .programming.
Please don't multi-post!
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
Query
I have a table with some product information. These products have related records in one more table with the manufacturing partno.
Each product has multiple manufacturing part no.
I want the product information and the manufacturing part no information
in the same line. If the manufacturing part no is one, then beside all the product info, the query should add one column and display the manufacturing no. And if the product has 2 manufacturing part no, then it should add 2 columns each having the manufacturing part no respectively.
I mean to say that there should be only one record for each product and beside the product info, it should have one/multiple fields showing the manufacturing model no.
Can anyone help me out how to do this query.
Thanks.You should do this on client part, not on server.|||Adding fields on the fly is not a good idea. What if there is an error in data entry or else where in the process of assigning manufacturer's part numbers to a product? Imagine the erroneous assignment resulted in 1500 part numbers associated with the same product, instead of 750 different ones? A better way would be to select distinct products, and in one (and only one) additional field have a comma-separated list of all associated manufacturer's part numbers, regardless of their number (well, preferrably where the numbers and the commas would fit into 8000-character field). For this to work all you need is a multi-statement function that returns varchar(8000) and takes product identifier as a parameter, so that it can be used in the same SELECT where you're retrieving distinct products.|||how do i start the function, unable to get any idea.
should i use a cursor or what?|||Suppose you have ProductID, ProductName, and PartNumber fields in your table (ProductsParts). For every PartNumber the values of ProductID and ProductName are repeated, right?
create function dbo.fn_PartNumbers (
@.ProductID int) returns varchar(8000) as
begin
declare @.PartNumbers varchar(8000)
set @.PartNumbers = ''
select @.PartNumbers = @.PartNumbers + PartNumber + ', '
from ProductsParts where ProductID = @.ProductID
return (substring(@.PartNumbers, 1, datalength(rtrim(@.PartNumbers))-1))
end
And then, here's your query:
select distinct ProductID, ProductName, PartNumbers = dbo.fn_PartNumbers(ProductID)
from ProductsParts|||Thank You very much. It worked out very well.
Thanks Again.
Query
Im new in database bussines. I will appreciate if someone can help me.
I need to insert new records to a table. First of all I dont know what is the syntax for insert command in SQL Server2000. Also one of the fields to insert is changing every second so I think I will need to include in the insert command a nested select statement.
For example,
The table has the ID field and this field is the one is changing every time. If in the n second I do a select to know what value such field has I could use the value equal to ID + 1. But while I build query, type and execute it will be the second n+1, so probably the value for ID now is already equal to ID + 1 and my query will fail because the duplicated record error.
I hope is clear what the proble I have,
Regards,You can look in BooksOnline (comes with the sql server) for the syntax on insert statements.
If your changing field is the ID for a record, you can use auto numbering on this field. This means that sql server itself will keep track of the value of the field and makes sure there are no duplicates.
hth|||So can I just use:
insert into table_name values(ID,'MNAME','F_NAME')...
I was thinking that some other query was needed to be introduced to get the new value for ID field, ie.
insert into table_name values(((select ID from TABLE)+1),'M_NAME','F_NAME')
Thanks|||Not completely correct yet, your insert statement. If the ID column is set to auto numbering, you don't include it in the insert statement. Your statement will be:
insert into table_name values('MNAME','F_NAME')|||Thanks, How I can know if the field is autonumbering?
How I can get the table fields with all it properties using SQL Command?
In db2 it was with command describe table...
It is the same?
Regards,|||in sql server, there is a stored proeedure for this information called 'sp_columns'. See BOL for more information. You can also find information about using auto numbering in BOL, just search on 'identity'.sql
Friday, March 23, 2012
Query
i have two tables that I want to match, the records that match I want to delete them from the original file and copy them in an other Tabl
Example
Table A Table
1A 3
3B 4
5J 1
All the records that match in that example (1A and 3B), I want to copy them in a new table and delete them only form TableB
I made a join between the two tables, and I have good results. But how to copy and delete them I don't know
Or maybe in Table B I can have a flag that I put on when it is a mached record? But that I nether don't know how to do that in my join query
Thks for help
JacTo copy them to a new table use SELECT.. INTO:
SELECT col1
INTO NewTable
FROM TableA JOIN TableB ON TableA.col1 = TableB.col1
To delete them:
DELETE TableB
FROM TableA JOIN TableB ON TableA.col1 = TableB.col1
--
Carlos E. Rojas
SQL Server MVP
Co-Author SQL Server 2000 Programming by Example
"Jac" <anonymous@.discussions.microsoft.com> wrote in message
news:4B32C95B-5EB4-4F21-8B58-EF0121E17D02@.microsoft.com...
> hey,
> i have two tables that I want to match, the records that match I want to
delete them from the original file and copy them in an other Table
> Example:
> Table A Table B
> 1A 3B
> 3B 4V
> 5J 1A
>
> All the records that match in that example (1A and 3B), I want to copy
them in a new table and delete them only form TableB.
> I made a join between the two tables, and I have good results. But how to
copy and delete them I don't know?
> Or maybe in Table B I can have a flag that I put on when it is a mached
record? But that I nether don't know how to do that in my join query.
> Thks for help.
> Jacsql
Query
i have two tables that I want to match, the records that match I want to del
ete them from the original file and copy them in an other Table
Example:
Table A Table B
1A 3B
3B 4V
5J 1A
All the records that match in that example (1A and 3B), I want to copy them
in a new table and delete them only form TableB.
I made a join between the two tables, and I have good results. But how to co
py and delete them I don't know?
Or maybe in Table B I can have a flag that I put on when it is a mached reco
rd? But that I nether don't know how to do that in my join query.
Thks for help.
JacTo copy them to a new table use SELECT.. INTO:
SELECT col1
INTO NewTable
FROM TableA JOIN TableB ON TableA.col1 = TableB.col1
To delete them:
DELETE TableB
FROM TableA JOIN TableB ON TableA.col1 = TableB.col1
Carlos E. Rojas
SQL Server MVP
Co-Author SQL Server 2000 programming by Example
"Jac" <anonymous@.discussions.microsoft.com> wrote in message
news:4B32C95B-5EB4-4F21-8B58-EF0121E17D02@.microsoft.com...
> hey,
> i have two tables that I want to match, the records that match I want to
delete them from the original file and copy them in an other Table
> Example:
> Table A Table B
> 1A 3B
> 3B 4V
> 5J 1A
>
> All the records that match in that example (1A and 3B), I want to copy
them in a new table and delete them only form TableB.
> I made a join between the two tables, and I have good results. But how to
copy and delete them I don't know?
> Or maybe in Table B I can have a flag that I put on when it is a mached
record? But that I nether don't know how to do that in my join query.
> Thks for help.
> Jac
Wednesday, March 21, 2012
Queries run faster with replication then without
I have a situation where I am using a Cursor to retrieve records from one table and inserting those records one at a time into another table. When I perform this action without replication, it takes longer to insert the 474 records into this table then it
does with replication. The table that is being inserted to is the table that is being replicated. It doesn't make sense. You would think that using replication would slow down this action. By contrast, if I use a simple INSERT statement without using a c
ursor (ie: Insert table2 select * from table1), the process runs much faster without replication then with replication. Any idea why?
Thanks!!
this is counter intuitive, unless the replication process is bringing the
database pages off disk and into cache thereby resulting in faster reads.
"Nupee" <anonymous@.discussions.microsoft.com> wrote in message
news:C5EDAB30-AB12-46BA-812B-1C0A6EB3562F@.microsoft.com...
> Hello everyone,
> I have a situation where I am using a Cursor to retrieve records from one
table and inserting those records one at a time into another table. When I
perform this action without replication, it takes longer to insert the 474
records into this table then it does with replication. The table that is
being inserted to is the table that is being replicated. It doesn't make
sense. You would think that using replication would slow down this action.
By contrast, if I use a simple INSERT statement without using a cursor (ie:
Insert table2 select * from table1), the process runs much faster without
replication then with replication. Any idea why?
> Thanks!!
|||Is it possible that it has something to do with the system procedures, and triggers that are added to the database through replication configuration? Either that or is there any documentation on the effects of replication of Cursors?
Thanks!!
-- Hilary Cotter wrote: --
this is counter intuitive, unless the replication process is bringing the
database pages off disk and into cache thereby resulting in faster reads.
"Nupee" <anonymous@.discussions.microsoft.com> wrote in message
news:C5EDAB30-AB12-46BA-812B-1C0A6EB3562F@.microsoft.com...[vbcol=seagreen]
> Hello everyone,
table and inserting those records one at a time into another table. When I
perform this action without replication, it takes longer to insert the 474
records into this table then it does with replication. The table that is
being inserted to is the table that is being replicated. It doesn't make
sense. You would think that using replication would slow down this action.
By contrast, if I use a simple INSERT statement without using a cursor (ie:
Insert table2 select * from table1), the process runs much faster without
replication then with replication. Any idea why?[vbcol=seagreen]
Monday, March 12, 2012
Qualified Joins
the common records with the lowest time.
Table1
recordID ArrivalTime
12345 12:01am
12346 12:30am
12347 12:45am
Table2
recordID ArrivalTime
12345 12:03am
12346 12:29am
12347 12:44am
The result should be
recordID ArrivalTime
12345 12:01am
12346 12:29am
12347 12:44am
Can someone point me in the right direction please?Hi Dave,
SELECT recordID,MIN(ArrivalTime)
FROM
(
SELECT recordID,ArrivalTime
FROM TABLE1
UNION
SELECT recordID,ArrivalTime
FROM TABLE2
) SubQuery
GROUP BY recordID
--OR
SELECT
recordID ,
CASE WHEN T1.ArrivalTime < T2.ArrivalTime THEN T1.ArrivalTime
ELSE T2.ArrivalTime END AS ArrivalTime
FROM TABLE1 T1
FULL OUTER JOIN TABLE2 T2
ON T1.recordid = T2.recordid
HTH, jens Suessmeyer.|||The 2nd query doesn't handle nulls...
SELECT
COALESCE( T1.recordid , T2.recordid ) AS recordid
, (CASE
WHEN T2.ArrivalTime IS NULL OR T1.ArrivalTime < T2.ArrivalTime ) THEN
T1.ArrivalTime
ELSE T2.ArrivalTime
END) AS ArrivalTime
FROM TABLE1 T1
FULL OUTER JOIN TABLE2 T2
ON T1.recordid = T2.recordid
"Jens" <Jens@.sqlserver2005.de> wrote in message
news:1131981403.240416.108000@.g47g2000cwa.googlegroups.com...
> Hi Dave,
>
> SELECT recordID,MIN(ArrivalTime)
> FROM
> (
> SELECT recordID,ArrivalTime
> FROM TABLE1
> UNION
> SELECT recordID,ArrivalTime
> FROM TABLE2
> ) SubQuery
> GROUP BY recordID
>
> --OR
> SELECT
> recordID ,
> CASE WHEN T1.ArrivalTime < T2.ArrivalTime THEN T1.ArrivalTime
> ELSE T2.ArrivalTime END AS ArrivalTime
> FROM TABLE1 T1
> FULL OUTER JOIN TABLE2 T2
> ON T1.recordid = T2.recordid
>
> HTH, jens Suessmeyer.
>
Friday, March 9, 2012
QA returns different records from SQL Server EM
If I run the following in Query Analyzer I get no records returned:
exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176, 183'
If, however, I run either :
exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176'
or
exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '183'
I get a single record returned in each case (which is what I would
expect).
The SQL for GetLeadsOutcome_Dealer is:
CREATE PROCEDURE GetLeadsOutcome_Dealer
@.FromDate smalldatetime,
@.ToDate smalldatetime,
@.OutcomeTypeID integer,
@.DealerCode varchar(8000)
AS
DECLARE @.TotalLeads integer
BEGIN
SELECT @.TotalLeads=COUNT(fldLeadID) FROM tblLeads WHERE
(tblLeads.fldDealerID IS NOT NULL)
SELECT DISTINCT (dbo.tblDealers.fldDealerName),
COUNT(dbo.tblLeads.fldLeadID) as LeadCount,@.TotalLeads AS TotalLeads
FROM tblLeads LEFT OUTER JOIN
tblOutcome ON tblLeads.fldOutcomeID =
tblOutcome.fldOutcomeID RIGHT OUTER JOIN
tblDealers RIGHT OUTER JOIN
tblRegion ON tblDealers.fldRegionID =
tblRegion.fldRegionID ON tblLeads.fldDealerID = tblDealers.fldDealerID
WHERE (((dbo.tblLeads.fldEntered) BETWEEN @.FromDate And @.ToDate)) AND
tblOutcome.fldOutcomeID=@.OutcomeTypeID AND
(dbo.tblDealers.fldDealerCode in (@.DealerCode))
GROUP BY dbo.tblDealers.fldDealerName
ORDER BY dbo.tblDealers.fldDealerName
END
GO
However, if I open EM, open a table in query view, and paste this into
the SQL window,
SELECT DISTINCT tblDealers.fldDealerName, COUNT(tblLeads.fldLeadID) AS
LeadCount
FROM tblLeads LEFT OUTER JOIN
tblOutcome ON tblLeads.fldOutcomeID =
tblOutcome.fldOutcomeID RIGHT OUTER JOIN
tblDealers RIGHT OUTER JOIN
tblRegion ON tblDealers.fldRegionID =
tblRegion.fldRegionID ON tblLeads.fldDealerID = tblDealers.fldDealerID
WHERE (tblLeads.fldEntered BETWEEN '1/1/2003' AND '12/2/2003') AND
(tblOutcome.fldOutcomeID = 10) AND (tblDealers.fldDealerCode IN (176,
183))
GROUP BY tblDealers.fldDealerName
ORDER BY tblDealers.fldDealerName
I get two records returned.
What is happening?
TIA
Edward
TABLE DEFS:
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_tblLeads_tblDealers]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[tblLeads] DROP CONSTRAINT FK_tblLeads_tblDealers
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_tblSystemUsers_tblDealers]') and
OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[tblSystemUsers] DROP CONSTRAINT
FK_tblSystemUsers_tblDealers
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_tblLeadNotes_tblLeads]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[tblLeadNotes] DROP CONSTRAINT
FK_tblLeadNotes_tblLeads
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_tblDealers_tblRegion]') and OBJECTPROPERTY(id,
N'IsForeignKey') = 1)
ALTER TABLE [dbo].[tblDealers] DROP CONSTRAINT FK_tblDealers_tblRegion
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblDealers]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[tblDealers]
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblLeads]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)
drop table [dbo].[tblLeads]
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblOutcome]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[tblOutcome]
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblRegion]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)
drop table [dbo].[tblRegion]
GO
CREATE TABLE [dbo].[tblDealers] (
[fldDealerID] [int] IDENTITY (1, 1) NOT NULL ,
[fldDealerCode] [varchar] (50) NOT NULL ,
[fldDealerName] [varchar] (50) NULL ,
[fldDealerTel] [varchar] (20) NULL ,
[fldDealerEmail] [varchar] (100) NULL ,
[fldDealerContact] [varchar] (50) NULL ,
[fldRegionID] [int] NULL ,
[fldDealerActive] [smallint] NOT NULL ,
[fldHeadOffice] [smallint] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tblLeads] (
[fldLeadID] [int] IDENTITY (1, 1) NOT NULL ,
[fldAccountNo] [varchar] (50) NULL ,
[fldDealerID] [int] NULL ,
[fldStatusID] [int] NOT NULL ,
[fldOutcomeID] [int] NOT NULL ,
[fldContacted] [smallint] NOT NULL ,
[fldDateContacted] [smalldatetime] NULL ,
[fldAppointment] [smallint] NULL ,
[fldShowRoom] [smallint] NOT NULL ,
[fldTestDrive] [smallint] NOT NULL ,
[fldSalesPersonID] [int] NULL ,
[fldCustomerName] [varchar] (50) NULL ,
[fldCHouseNo] [varchar] (50) NULL ,
[fldCStreet] [varchar] (50) NULL ,
[fldCDistrict] [varchar] (50) NULL ,
[fldCTown] [varchar] (40) NULL ,
[fldCCounty] [varchar] (50) NULL ,
[fldCPostcode] [varchar] (50) NULL ,
[fldPhoneInd] [varchar] (20) NULL ,
[fldCTel] [varchar] (50) NULL ,
[fldNewBusinessDate] [smalldatetime] NULL ,
[fldAgreementType] [varchar] (50) NULL ,
[fldCashPrice] [smallmoney] NULL ,
[fldBalanceFin] [smallmoney] NULL ,
[fldCustRate] [varchar] (10) NULL ,
[fldOrigTerm] [int] NULL ,
[fldPPPType] [varchar] (10) NULL ,
[fldBalloonValue] [smallmoney] NULL ,
[fldMonthlyInstal] [smallmoney] NULL ,
[fldRegNo] [varchar] (10) NULL ,
[fldRegDate] [smalldatetime] NULL ,
[fldModel] [varchar] (50) NULL ,
[fldDescription] [varchar] (50) NULL ,
[fldTheoPIFDate] [smalldatetime] NULL ,
[fldMonthsToGo] [int] NULL ,
[fldBalanceOS] [smallmoney] NULL ,
[fldLeadPrinted] [smallint] NULL ,
[fldMergeFile] [varchar] (255) NULL ,
[fldTPS] [varchar] (10) NULL ,
[fldMPS] [varchar] (10) NULL ,
[fldUpdated] [smalldatetime] NULL ,
[fldUpdatedBy] [int] NULL ,
[fldEntered] [smalldatetime] NULL ,
[fldReasonLeadNotProgressed] [varchar] (5000) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tblOutcome] (
[fldOutcomeID] [int] IDENTITY (1, 1) NOT NULL ,
[fldOutcomeCode] [int] NULL ,
[fldOutcome] [varchar] (100) NULL ,
[fldConvertedSales] [smallint] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tblRegion] (
[fldRegionID] [int] IDENTITY (1, 1) NOT NULL ,
[fldRegionCode] [varchar] (10) NULL ,
[fldRegion] [varchar] (50) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblDealers] WITH NOCHECK ADD
CONSTRAINT [PK_tblDealers] PRIMARY KEY CLUSTERED
(
[fldDealerID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblLeads] WITH NOCHECK ADD
CONSTRAINT [PK_tblLeads] PRIMARY KEY CLUSTERED
(
[fldLeadID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblOutcome] WITH NOCHECK ADD
CONSTRAINT [PK_tblOutcome] PRIMARY KEY CLUSTERED
(
[fldOutcomeID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblRegion] WITH NOCHECK ADD
CONSTRAINT [PK_tblArea] PRIMARY KEY CLUSTERED
(
[fldRegionID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblDealers] WITH NOCHECK ADD
CONSTRAINT [DF_tblDealers_fldActive] DEFAULT ((-1)) FOR
[fldDealerActive],
CONSTRAINT [DF_tblDealers_fldHeadOffice] DEFAULT (0) FOR
[fldHeadOffice],
CONSTRAINT [IX_tblDealers] UNIQUE NONCLUSTERED
(
[fldDealerCode]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblLeads] WITH NOCHECK ADD
CONSTRAINT [DF_tblLeads_fldOutcomeID] DEFAULT (10) FOR
[fldOutcomeID],
CONSTRAINT [DF_tblLeads_fldContacted] DEFAULT (0) FOR [fldContacted],
CONSTRAINT [DF_tblLeads_fldAppointment] DEFAULT (0) FOR
[fldAppointment],
CONSTRAINT [DF_tblLeads_fldShowRoom] DEFAULT (0) FOR [fldShowRoom],
CONSTRAINT [DF_tblLeads_fldTestDrive] DEFAULT (0) FOR [fldTestDrive],
CONSTRAINT [DF_tblLeads_fldLeadPrinted] DEFAULT (0) FOR
[fldLeadPrinted],
CONSTRAINT [DF_tblLeads_fldTPSMatch] DEFAULT ('NO') FOR [fldTPS],
CONSTRAINT [DF_tblLeads_fldMPSMatch] DEFAULT ('NO') FOR [fldMPS]
GO
ALTER TABLE [dbo].[tblOutcome] WITH NOCHECK ADD
CONSTRAINT [DF_tblOutcome_fldConvertedSales] DEFAULT (0) FOR
[fldConvertedSales]
GO
ALTER TABLE [dbo].[tblRegion] WITH NOCHECK ADD
CONSTRAINT [IX_tblRegion] UNIQUE NONCLUSTERED
(
[fldRegionCode]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblDealers] ADD
CONSTRAINT [FK_tblDealers_tblRegion] FOREIGN KEY
(
[fldRegionID]
) REFERENCES [dbo].[tblRegion] (
[fldRegionID]
)
GO
ALTER TABLE [dbo].[tblLeads] ADD
CONSTRAINT [FK_tblLeads_tblDealers] FOREIGN KEY
(
[fldDealerID]
) REFERENCES [dbo].[tblDealers] (
[fldDealerID]
)
GO
alter table [dbo].[tblLeads] nocheck constraint
[FK_tblLeads_tblDealers]
GOEdward (teddysnips@.hotmail.com) writes:
> If I run the following in Query Analyzer I get no records returned:
> exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176, 183'
>...
> The SQL for GetLeadsOutcome_Dealer is:
> ...
> (dbo.tblDealers.fldDealerCode in (@.DealerCode))
Apparently, you dont have any dealer with the code '176, 183'.
> However, if I open EM, open a table in query view, and paste this into
> the SQL window,
>...
> (tblDealers.fldDealerCode IN (176, 183))
Here you have two different values. In the procedure you have only one.
Looks like you need a procedure that unpacks a list of values into
a table. Look at
http://www.algonet.se/~sommar/array...-of-string-proc.
(This brings you to a procedure which is for SQL7. But you have have
to read some other parts of the long text to make sense of it.)
--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||teddysnips@.hotmail.com (Edward) wrote in message news:<25080b60.0312020231.449732c@.posting.google.com>...
> SQL Server 7.0
> If I run the following in Query Analyzer I get no records returned:
> exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176, 183'
> If, however, I run either :
> exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176'
> or
> exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '183'
> I get a single record returned in each case (which is what I would
> expect).
<snip
Your first SQL statement essentially comes down to this:
select * from table where column in ('183, 176')
In other words, return the columns with a value of '183, 176'. Since
there aren't any, you get no rows. What you are trying to achieve is
this - return columns with a value of '183' or '176':
select * from table where column in ('183', '176')
When you use a single value for the variable, you get a valid query,
and hence a single result:
select * from table where column in ('183')
You're getting results in EM because you've correctly typed in the
values manually.
See here for a fuller discussion:
http://www.algonet.se/~sommar/dynamic_sql.html#List
Simon