Showing posts with label below. Show all posts
Showing posts with label below. Show all posts

Wednesday, March 28, 2012

Query : Running a job/step in a loop for ALL databases

Hi,

I've written a job to export user and database permissions for all
d/b's on a server. As you can see below, the T-SQL commands are the
same for each d/b. Can anyone assist with regard to re-writing this so
that any new d/b's added do not require ammending the job (loop)?

Thx,

GC.

use master
go
SELECT db_name()
EXEC sp_helpuser
EXEC sp_helprotect NULL, NULL, NULL, 'o s'
use msdb
go
SELECT db_name()
EXEC sp_helpuser
EXEC sp_helprotect NULL, NULL, NULL, 'o s'
use test1
go
SELECT db_name()
EXEC sp_helpuser
EXEC sp_helprotect NULL, NULL, NULL, 'o s'
use test2
go
SELECT db_name()
EXEC sp_helpuser
EXEC sp_helprotect NULL, NULL, NULL, 'o s'"Garry Clarke" <gclarke@.euro.banta.com> wrote in message
news:fed38413.0310240324.77f4ce60@.posting.google.c om...
> Hi,
> I've written a job to export user and database permissions for all
> d/b's on a server. As you can see below, the T-SQL commands are the
> same for each d/b. Can anyone assist with regard to re-writing this so
> that any new d/b's added do not require ammending the job (loop)?
> Thx,
> GC.
> use master
> go
> SELECT db_name()
> EXEC sp_helpuser
> EXEC sp_helprotect NULL, NULL, NULL, 'o s'
> use msdb
> go
> SELECT db_name()
> EXEC sp_helpuser
> EXEC sp_helprotect NULL, NULL, NULL, 'o s'
> use test1
> go
> SELECT db_name()
> EXEC sp_helpuser
> EXEC sp_helprotect NULL, NULL, NULL, 'o s'
> use test2
> go
> SELECT db_name()
> EXEC sp_helpuser
> EXEC sp_helprotect NULL, NULL, NULL, 'o s'

A cursor is one way to do this (cursors are usually a bad idea in
application code, but can be useful for admin scripts):

declare @.db sysname

declare cur_dbs cursor fast_forward
for select name from master..sysdatabases
order by name

open cur_dbs

fetch next from cur_dbs into @.db

while @.@.fetch_status = 0
begin
select @.db
exec('exec ' + @.db + '..sp_helpuser')
exec('exec ' +@.db + '..sp_helprotect NULL, NULL, NULL, ''os''')
fetch next from cur_dbs into @.db
end

close cur_dbs
deallocate cur_dbs

Simon

Monday, March 26, 2012

Query

Hi

I am trying to execute the below query in Query analyzer, the same query is executed in Sybase with the corresponding columns.

select (a.OnlineAccessId) + CASE
WHEN (a.onlineaccessid2) <> '00000000000000' THEN (a.onlineaccessid2)
else ''
End + CASE
WHEN (a.onlineaccessid3) <> '00000000000000' THEN (a.onlineaccessid3)
else ''
End as onlineaccessids, a.id as AcctInfoId, BankNo, RegionNo, OfficeNo, AcctNo,AcctShortNm ,SecLending, q.id as UsersId
from AcctInfo AS a INNER JOIN "104030" AS p ON a.AdminOfficer = p.CrmCode
inner join Users as q ON p.CrmDesc = q.OnlineaccessId union select
OnlineAccessIds = (a.OnlineAccessId) + CASE
WHEN (a.onlineaccessid2) <> '00000000000000' THEN (a.onlineaccessid2)
else ''
End + CASE
WHEN (a.onlineaccessid3) <> '00000000000000' THEN (a.onlineaccessid3)
else ''
End,q.id, a.Id, BankNo, RegionNo,
OfficeNo, AcctNo,AcctShortNm, SecLending
from AcctInfo as a
inner join users as q on
(q.onlineaccessid = substring(onlineaccessids, 1, 3))

The error message is

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'onlineaccessids'..

It is able to recognize the field "onlineaccessids" and giving me the results in Sybase database and giving the error in SQL Server.
Try to help me out.

Thanx in advanceFirst, double check that both the AcctInfo and users tables have an attribute "onlineaccessids".

Secondly, add an alias to:

(q.onlineaccessid = substring(a/q.onlineaccessids, 1, 3))

query


dat temp
ani 2
fruit 3
ani 1
clothes 4
ani 3
i want to make below that from 1 table(name:tbl) upper
aniCnt clothesCnt fruitCnt
3 1 1
how make query statement?
*** Sent via Developersdex http://www.examnotes.net ***Try using a "case" expression.
select
sum(case when dat = 'ani' then 1 else 0 end) as aniCnt,
sum(case when dat = 'fruit' then 1 else 0 end) as fruitCnt,
sum(case when dat = 'clothes' then 1 else 0 end) as clothesCnt
from
tbl
go
HOW TO: Rotate a Table in SQL Server
http://support.microsoft.com/defaul...574&Product=sql
Dynamic Crosstab Queries
http://www.windowsitpro.com/SQLServ...5608/15608.html
Dynamic Cross-Tabs/Pivot Tables
http://www.sqlteam.com/item.asp?ItemID=2955
AMB
"tom taol" wrote:

>
> dat temp
> ani 2
> fruit 3
> ani 1
> clothes 4
> ani 3
> i want to make below that from 1 table(name:tbl) upper
> aniCnt clothesCnt fruitCnt
> 3 1 1
> how make query statement?
>
> *** Sent via Developersdex http://www.examnotes.net ***
>|||Try code below
create table #temp (dat varchar(10), [temp] int)
declare @.sql varchar(8000)
insert into #temp
select 'ani', 2 union all
select 'fruit', 3 union all
select 'ani', 1 union all
select 'clothes', 4 union all
select 'ani', 3
select @.sql = coalesce(@.sql + ', ', 'select ') + convert(varchar,
count([temp])) + ' [' + dat + 'Cnt]'
from #temp
group by
dat
exec (@.sql)
drop table #temp
Rakesh
"tom taol" wrote:

>
> dat temp
> ani 2
> fruit 3
> ani 1
> clothes 4
> ani 3
> i want to make below that from 1 table(name:tbl) upper
> aniCnt clothesCnt fruitCnt
> 3 1 1
> how make query statement?
>
> *** Sent via Developersdex http://www.examnotes.net ***
>

Friday, March 23, 2012

query

Hi,
There is a table which contains the rows and I would like to create a query
that can show the below result. Can a query do that?
Thanks
Table
--
Owner Cat Name
1 1 Kitap
1 2 Defter
1 3 Kalem
1 2 Dergi
2 1 Ceket
2 1 Gmlek
2 2 Kravat
2 3 orap
2 3 Pantolon
---
The Query result
Owner Cat Name
1 1 Kitap
1 2 Defter
1 3 Kalem
2 1 Ceket
2 2 Kravat
2 3 orap
---A query can probably do this, but you will have to tell us
how you decide which Name to return when there is more
than one row with the same Owner and Cat values. Here,
it looks like you are choosing the one that appears first in
the list of all table rows, but in order to do this with a query,
you need to define the row you want in terms of the column
values, not a particular output order that you can't rely on.
Steve Kass
Drew University
tolgay wrote:

>Hi,
>There is a table which contains the rows and I would like to create a query
>that can show the below result. Can a query do that?
>Thanks
>
>Table
>--
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 1 2 Dergi
> 2 1 Ceket
> 2 1 G闣lek
> 2 2 Kravat
> 2 3 よrap
> 2 3 Pantolon
>---
>The Query result
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 2 1 Ceket
> 2 2 Kravat
> 2 3 よrap
>---
>
>|||Actually it doesn't matter which row comes with the query. But the main
point is the query must get one of the rows.
Thanks
"Steve Kass" <skass@.drew.edu> wrote in message
news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
> A query can probably do this, but you will have to tell us
> how you decide which Name to return when there is more
> than one row with the same Owner and Cat values. Here,
> it looks like you are choosing the one that appears first in
> the list of all table rows, but in order to do this with a query,
> you need to define the row you want in terms of the column
> values, not a particular output order that you can't rely on.
> Steve Kass
> Drew University
> tolgay wrote:
>
query|||in that case...
Select owner
, cat
, max(name)
from CatTable
group by owner
, cat
However, this table structure needs keys to prevent duplicates. Two rows as
shown here should never exist. You probably want to change your database
structures before doing anything else. You can guarantee that the name you
pull back will be wrong half the time.
"tolgay" <tgul@.tgul.com> wrote in message
news:%23DJTKpFcGHA.1276@.TK2MSFTNGP03.phx.gbl...
> Actually it doesn't matter which row comes with the query. But the main
> point is the query must get one of the rows.
> Thanks
> "Steve Kass" <skass@.drew.edu> wrote in message
> news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
> query
>|||Jim has offered a solution, and I'll just add that while
it doesn't matter to you, you still have to tell SQL Server
what to do. There's no ANY_OLD_ONE aggregate
in SQL. :)
SK
tolgay wrote:

>Actually it doesn't matter which row comes with the query. But the main
>point is the query must get one of the rows.
>Thanks
>"Steve Kass" <skass@.drew.edu> wrote in message
>news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
>
>query
>
>
>|||thanks a lot you saved my day :)
"tolgay" <tgul@.tgul.com> wrote in message
news:un$oOaFcGHA.3856@.TK2MSFTNGP03.phx.gbl...
> Hi,
> There is a table which contains the rows and I would like to create a
> query
> that can show the below result. Can a query do that?
> Thanks
>
> Table
> --
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 1 2 Dergi
> 2 1 Ceket
> 2 1 Gmlek
> 2 2 Kravat
> 2 3 orap
> 2 3 Pantolon
> ---
> The Query result
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 2 1 Ceket
> 2 2 Kravat
> 2 3 orap
> ---
>sql

query

Hi,
There is a table which contains the rows and I would like to create a query
that can show the below result. Can a query do that?
Thanks
Table
--
Owner Cat Name
1 1 Kitap
1 2 Defter
1 3 Kalem
1 2 Dergi
2 1 Ceket
2 1 Gömlek
2 2 Kravat
2 3 Çorap
2 3 Pantolon
---
The Query result
Owner Cat Name
1 1 Kitap
1 2 Defter
1 3 Kalem
2 1 Ceket
2 2 Kravat
2 3 Çorap
---A query can probably do this, but you will have to tell us
how you decide which Name to return when there is more
than one row with the same Owner and Cat values. Here,
it looks like you are choosing the one that appears first in
the list of all table rows, but in order to do this with a query,
you need to define the row you want in terms of the column
values, not a particular output order that you can't rely on.
Steve Kass
Drew University
tolgay wrote:
>Hi,
>There is a table which contains the rows and I would like to create a query
>that can show the below result. Can a query do that?
>Thanks
>
>Table
>--
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 1 2 Dergi
> 2 1 Ceket
> 2 1 Gé?£lek
> 2 2 Kravat
> 2 3 ã'rap
> 2 3 Pantolon
>---
>The Query result
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 2 1 Ceket
> 2 2 Kravat
> 2 3 ã'rap
>---
>
>|||Actually it doesn't matter which row comes with the query. But the main
point is the query must get one of the rows.
Thanks
"Steve Kass" <skass@.drew.edu> wrote in message
news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
> A query can probably do this, but you will have to tell us
> how you decide which Name to return when there is more
> than one row with the same Owner and Cat values. Here,
> it looks like you are choosing the one that appears first in
> the list of all table rows, but in order to do this with a query,
> you need to define the row you want in terms of the column
> values, not a particular output order that you can't rely on.
> Steve Kass
> Drew University
> tolgay wrote:
> >Hi,
> >
> >There is a table which contains the rows and I would like to create a
query
> >that can show the below result. Can a query do that?
> >Thanks
> >
> >
> >Table
> >--
> > Owner Cat Name
> > 1 1 Kitap
> > 1 2 Defter
> > 1 3 Kalem
> > 1 2 Dergi
> > 2 1 Ceket
> > 2 1 G?lek
> > 2 2 Kravat
> > 2 3 ?rap
> > 2 3 Pantolon
> >
> >---
> >The Query result
> >
> > Owner Cat Name
> > 1 1 Kitap
> > 1 2 Defter
> > 1 3 Kalem
> > 2 1 Ceket
> > 2 2 Kravat
> > 2 3 ?rap
> >
> >---
> >
> >
> >
> >|||in that case...
Select owner
, cat
, max(name)
from CatTable
group by owner
, cat
However, this table structure needs keys to prevent duplicates. Two rows as
shown here should never exist. You probably want to change your database
structures before doing anything else. You can guarantee that the name you
pull back will be wrong half the time.
"tolgay" <tgul@.tgul.com> wrote in message
news:%23DJTKpFcGHA.1276@.TK2MSFTNGP03.phx.gbl...
> Actually it doesn't matter which row comes with the query. But the main
> point is the query must get one of the rows.
> Thanks
> "Steve Kass" <skass@.drew.edu> wrote in message
> news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
> > A query can probably do this, but you will have to tell us
> > how you decide which Name to return when there is more
> > than one row with the same Owner and Cat values. Here,
> > it looks like you are choosing the one that appears first in
> > the list of all table rows, but in order to do this with a query,
> > you need to define the row you want in terms of the column
> > values, not a particular output order that you can't rely on.
> >
> > Steve Kass
> > Drew University
> >
> > tolgay wrote:
> >
> > >Hi,
> > >
> > >There is a table which contains the rows and I would like to create a
> query
> > >that can show the below result. Can a query do that?
> > >Thanks
> > >
> > >
> > >Table
> > >--
> > > Owner Cat Name
> > > 1 1 Kitap
> > > 1 2 Defter
> > > 1 3 Kalem
> > > 1 2 Dergi
> > > 2 1 Ceket
> > > 2 1 G?lek
> > > 2 2 Kravat
> > > 2 3 ?rap
> > > 2 3 Pantolon
> > >
> > >---
> > >The Query result
> > >
> > > Owner Cat Name
> > > 1 1 Kitap
> > > 1 2 Defter
> > > 1 3 Kalem
> > > 2 1 Ceket
> > > 2 2 Kravat
> > > 2 3 ?rap
> > >
> > >---
> > >
> > >
> > >
> > >
>|||Jim has offered a solution, and I'll just add that while
it doesn't matter to you, you still have to tell SQL Server
what to do. There's no ANY_OLD_ONE aggregate
in SQL. :)
SK
tolgay wrote:
>Actually it doesn't matter which row comes with the query. But the main
>point is the query must get one of the rows.
>Thanks
>"Steve Kass" <skass@.drew.edu> wrote in message
>news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
>
>>A query can probably do this, but you will have to tell us
>>how you decide which Name to return when there is more
>>than one row with the same Owner and Cat values. Here,
>>it looks like you are choosing the one that appears first in
>>the list of all table rows, but in order to do this with a query,
>>you need to define the row you want in terms of the column
>>values, not a particular output order that you can't rely on.
>>Steve Kass
>>Drew University
>>tolgay wrote:
>>
>>Hi,
>>There is a table which contains the rows and I would like to create a
>>
>query
>
>>that can show the below result. Can a query do that?
>>Thanks
>>
>>Table
>>--
>> Owner Cat Name
>> 1 1 Kitap
>> 1 2 Defter
>> 1 3 Kalem
>> 1 2 Dergi
>> 2 1 Ceket
>> 2 1 G?lek
>> 2 2 Kravat
>> 2 3 ?rap
>> 2 3 Pantolon
>>---
>>The Query result
>> Owner Cat Name
>> 1 1 Kitap
>> 1 2 Defter
>> 1 3 Kalem
>> 2 1 Ceket
>> 2 2 Kravat
>> 2 3 ?rap
>>---
>>
>>
>>
>
>|||thanks a lot you saved my day :)
"tolgay" <tgul@.tgul.com> wrote in message
news:un$oOaFcGHA.3856@.TK2MSFTNGP03.phx.gbl...
> Hi,
> There is a table which contains the rows and I would like to create a
> query
> that can show the below result. Can a query do that?
> Thanks
>
> Table
> --
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 1 2 Dergi
> 2 1 Ceket
> 2 1 Gömlek
> 2 2 Kravat
> 2 3 Çorap
> 2 3 Pantolon
> ---
> The Query result
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 2 1 Ceket
> 2 2 Kravat
> 2 3 Çorap
> ---
>

query

Hi,
There is a table which contains the rows and I would like to create a query
that can show the below result. Can a query do that?
Thanks
Table
--
Owner Cat Name
1 1 Kitap
1 2 Defter
1 3 Kalem
1 2 Dergi
2 1 Ceket
2 1 Gmlek
2 2 Kravat
2 3 orap
2 3 Pantolon
---
The Query result
Owner Cat Name
1 1 Kitap
1 2 Defter
1 3 Kalem
2 1 Ceket
2 2 Kravat
2 3 orap
---A query can probably do this, but you will have to tell us
how you decide which Name to return when there is more
than one row with the same Owner and Cat values. Here,
it looks like you are choosing the one that appears first in
the list of all table rows, but in order to do this with a query,
you need to define the row you want in terms of the column
values, not a particular output order that you can't rely on.
Steve Kass
Drew University
tolgay wrote:

>Hi,
>There is a table which contains the rows and I would like to create a query
>that can show the below result. Can a query do that?
>Thanks
>
>Table
>--
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 1 2 Dergi
> 2 1 Ceket
> 2 1 G闣lek
> 2 2 Kravat
> 2 3 よrap
> 2 3 Pantolon
>---
>The Query result
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 2 1 Ceket
> 2 2 Kravat
> 2 3 よrap
>---
>
>|||Actually it doesn't matter which row comes with the query. But the main
point is the query must get one of the rows.
Thanks
"Steve Kass" <skass@.drew.edu> wrote in message
news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...[vbcol=seagreen]
> A query can probably do this, but you will have to tell us
> how you decide which Name to return when there is more
> than one row with the same Owner and Cat values. Here,
> it looks like you are choosing the one that appears first in
> the list of all table rows, but in order to do this with a query,
> you need to define the row you want in terms of the column
> values, not a particular output order that you can't rely on.
> Steve Kass
> Drew University
> tolgay wrote:
>
query[vbcol=seagreen]|||in that case...
Select owner
, cat
, max(name)
from CatTable
group by owner
, cat
However, this table structure needs keys to prevent duplicates. Two rows as
shown here should never exist. You probably want to change your database
structures before doing anything else. You can guarantee that the name you
pull back will be wrong half the time.
"tolgay" <tgul@.tgul.com> wrote in message
news:%23DJTKpFcGHA.1276@.TK2MSFTNGP03.phx.gbl...
> Actually it doesn't matter which row comes with the query. But the main
> point is the query must get one of the rows.
> Thanks
> "Steve Kass" <skass@.drew.edu> wrote in message
> news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
> query
>|||Jim has offered a solution, and I'll just add that while
it doesn't matter to you, you still have to tell SQL Server
what to do. There's no ANY_OLD_ONE aggregate
in SQL.
SK
tolgay wrote:

>Actually it doesn't matter which row comes with the query. But the main
>point is the query must get one of the rows.
>Thanks
>"Steve Kass" <skass@.drew.edu> wrote in message
>news:u6PVjiFcGHA.3380@.TK2MSFTNGP04.phx.gbl...
>
>query
>
>
>|||thanks a lot you saved my day
"tolgay" <tgul@.tgul.com> wrote in message
news:un$oOaFcGHA.3856@.TK2MSFTNGP03.phx.gbl...
> Hi,
> There is a table which contains the rows and I would like to create a
> query
> that can show the below result. Can a query do that?
> Thanks
>
> Table
> --
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 1 2 Dergi
> 2 1 Ceket
> 2 1 Gmlek
> 2 2 Kravat
> 2 3 orap
> 2 3 Pantolon
> ---
> The Query result
> Owner Cat Name
> 1 1 Kitap
> 1 2 Defter
> 1 3 Kalem
> 2 1 Ceket
> 2 2 Kravat
> 2 3 orap
> ---
>

Wednesday, March 21, 2012

Queries with different results...

Hi,
I am getting a difference of '1' when I am running the queries which I am listing below...
1)
select count(distinct(I.order_ID)) as completedTotal
from T_INVOICES I, T_ORDERS O,T_STATUS S
where
O.order_Id = I.order_ID

and I.feeDate >= {d '2002-08-01'} and I.feeDate < {d '2002-09-01'}
AND client_ID NOT IN(14,693) AND NOT EXISTS(SELECT Order_ID FROM T_STATUS WHERE
order_id = O.order_ID AND StatusType_ID = 7)
__________________________________________________ __________
2)
select count(distinct(I.order_ID)) as completedTotal
from T_INVOICES I
LEFT OUTER JOIN T_ORDERS AS O ON (O.order_Id = I.order_ID)
LEFT OUTER JOIN T_STATUS AS S ON (s.Order_ID = O.order_id)

WHERE I.feeDate >= {d '2002-08-01'} and I.feeDate < {d '2002-09-01'}
AND client_ID NOT IN(14,693) AND NOT EXISTS(SELECT Order_ID FROM T_STATUS WHERE order_id = O.order_ID AND StatusType_ID = 7)

can anyone help inresolving this.

thanksIn your first query, table S is not joined to table O, resulting in a cross-join.

blindman|||could you say how to optimize the second query?|||Well, I would script it like this for clarity, but this is just my style (it looks better once you paste it without the line-wrapping):

select count(distinct(I.order_ID)) as completedTotal
from T_INVOICES
LEFT OUTER JOIN T_ORDERS on T_ORDERS.order_Id = T_INVOICES.order_ID
LEFT OUTER JOIN T_STATUS on T_STATUS.Order_ID = T_ORDERS.order_id
WHERE T_INVOICES.feeDate '2002-08-01' and T_INVOICES.feeDate < '2002-09-01'
AND client_ID NOT IN(14,693)
AND NOT EXISTS (SELECT Order_ID FROM T_STATUS WHERE order_id = T_ORDERS.order_ID AND StatusType_ID = 7)

I've never liked using short aliases for tables because I've never thought the time saved in typing was worth the cost in readability.

You can optimize your query by indexing the join columns and criteria columns: order_id, feeDate, client_ID, and StatusType, but it may not be necessary to index all of them to get good performance.

Make sure you understand how your NOT EXISTS clause is going to affect your output. It will exclude all invoice and order records where there is a status recrod = 7, even if there are additional associated status records that do not = 7. If you just want to exclude status records = 7 from your output, put your criteria in the join:

select count(distinct(I.order_ID)) as completedTotal
from T_INVOICES
LEFT OUTER JOIN T_ORDERS on T_ORDERS.order_Id = T_INVOICES.order_ID
LEFT OUTER JOIN T_STATUS on T_STATUS.Order_ID = T_ORDERS.order_id T_STATUS.StatusType_ID <> 7
WHERE T_INVOICES.feeDate '2002-08-01' and T_INVOICES.feeDate < '2002-09-01'
AND client_ID NOT IN(14,693)

Also, I don't know how you have your relationships set up, but by the way you have three tables joined on order_id I suspect you are violating database normalization guidelines. (Unless the orders table has a one-to-many relationship with both Invoices and Status tables.)

blindman|||Hi,
I am using the query,

select count(distinct(T_INVOICES.order_ID)) as completedTotal
from T_INVOICES
LEFT OUTER JOIN T_ORDERS on T_ORDERS.order_Id = T_INVOICES.order_ID
LEFT OUTER JOIN T_STATUS on T_STATUS.Order_ID = T_ORDERS.order_id and T_STATUS.StatusType_ID <> 7
where
T_INVOICES.feeDate >= {d '2002-08-01'} and T_INVOICES.feeDate < {d '2002-09-01'}
AND client_ID NOT IN(14,693)
__________________________________________________ ___

but i am getting the number so high of the actual. Actually I have to get '29' instead I am getting '46'. Can you explain it.

thanks,
siva|||Instead of:
select count(distinct(T_INVOICES.order_ID)) as completedTotal

try this:
select distinct T_INVOICES.order_ID

...in order to see what data is actually being counted. I'm not sure your count(distinct( syntax is going to work the way you are expecting it to.

Also, try this:
select count(distinct T_INVOICES.order_ID) as completedTotal

The extra parentheses you have aren't necessary and may be affected your results.

blindman|||still iam getting the same result...|||So how many rows were returned by "select distinct T_INVOICES.order_ID"? Were any of them duplicates? Were any there that weren't supposed to be there?

Your problem my be due to, or compounded by, the relationships established between your tables. What is the architecture of these three tables:

Invoices -> Orders -> Status?

or

Invoices <- Orders -> Status?

You may need to try building your query from scratch again, starting with a simple select from Invoices and then adding joins and criteria as you verify that you are getting the data you expect. Your query design has some subtle joins and criteria, but I can't tell if these are required to get the results you want, or if they are just coding issues to be cleaned up.

blindmansql

Queries using bitwise?

Hi Everyone,
I've using the query below to extract some information. ONe of the items in
the where clause is a check for an integer column for 0x1.
Question: What is the correct syntax for checking this column? As below, I
have pm.ProductStatus = 0x1. This is returning rows. Am I correct in this?
Thanks in advance
Larry
SELECT am.AccountID, cpc.ProductID, cpc.ProductName, cpc.ManufacturerName,
cpc.ManufacturerPartNumber, wtl.Description, cpws.Notes
FROM
Account_Master am
INNER JOIN ClientProductCatalog cpc
ON am.AccountID = cpc.AccountID
INNER JOIN ProductMaster pm
ON cpc.ProductID = pm.ProductID
INNER JOIN ClientProductWorkflowStatus cpws
ON cpc.ClientProductCatalogID = cpws.clientProductCatalogID
INNER JOIN WorkflowStatusTypeLkp wfst
ON cpws.WorkflowStatusTypeLkpID = wfst.WorkFlowStatusTypeLkpID
INNER JOIN WorkflowTypeLkp wtl
ON cpws.WorkflowTypeLkpID = wtl.WorkflowTypeLkpID
WHERE wfst.WorkflowStatusTypeLkpID = 3 -- Needs more information from
customer?
AND cpc.Deactivated = 0 -- Product still active?
AND pm.ProductStatus = 0x1 -- Awaiting status?> Question: What is the correct syntax for checking this column? As below,
I
> have pm.ProductStatus = 0x1. This is returning rows. Am I correct in
this?
The equal operator is NOT a bitwise operator. Therefore, the answer is "NO"
if you are attempting to limit the resultset to rows where ProductStatus has
the lowest bit set. If this is actually what you are after, then this
highlights a potential flaw in your design. Product status appears to
contain multiple pieces of information in a single column, a violation of
first normal form. On the flip side, perhaps you want status values that are
odd?
Assuming you want to continue down this path, then have a look in BOL for
the bitwise operators. As a hint, you will need to use the bitwise AND
operator, using an expression in the form of "(mycolumn AND y) = z" (note -
pseudocode).|||Larry,
How can we know if this is the correct syntax to check if you don't
say what you are trying to check? The query you posted will select
rows where pm.productStatus equals 1. If that's what you want, then
that's what you'll get. If that's not what you want, you need to tell us
what it is that you want. You say the query is returning rows. Have
you tried to run this on some test data to see if they are the rows you
want?
It's not clear why you are expressing the number 1 as 0x1, by the way.
The query you are posting here would be easier to read if you wrote
pm.productStatus = 1.
Please provide more information.
Larry wrote:

>Hi Everyone,
>I've using the query below to extract some information. ONe of the items i
n
>the where clause is a check for an integer column for 0x1.
>Question: What is the correct syntax for checking this column? As below,
I
>have pm.ProductStatus = 0x1. This is returning rows. Am I correct in this
?
>Thanks in advance
>Larry
>
>SELECT am.AccountID, cpc.ProductID, cpc.ProductName, cpc.ManufacturerName,
>cpc.ManufacturerPartNumber, wtl.Description, cpws.Notes
>FROM
>Account_Master am
>INNER JOIN ClientProductCatalog cpc
>ON am.AccountID = cpc.AccountID
>INNER JOIN ProductMaster pm
>ON cpc.ProductID = pm.ProductID
>INNER JOIN ClientProductWorkflowStatus cpws
>ON cpc.ClientProductCatalogID = cpws.clientProductCatalogID
>INNER JOIN WorkflowStatusTypeLkp wfst
>ON cpws.WorkflowStatusTypeLkpID = wfst.WorkFlowStatusTypeLkpID
>INNER JOIN WorkflowTypeLkp wtl
>ON cpws.WorkflowTypeLkpID = wtl.WorkflowTypeLkpID
>WHERE wfst.WorkflowStatusTypeLkpID = 3 -- Needs more information from
>customer?
>AND cpc.Deactivated = 0 -- Product still active?
>AND pm.ProductStatus = 0x1 -- Awaiting status?
>|||Larry - The bitwise operators (from BOL) are
bitwise logical And ... &
bitwise logical or ... |
bitwise NOT ............ ~
bitwise exclusive or ... ^
So to test if an integral value has a particular bit SET
(Defined by a bitmask with all zeros but one)
Bitmask must be decimal 1,2,4,8,16, etc, or hex 0x1, 0x2, 0x4, 0x8, 0x10,
0x20, etc
use logical And ....... Where (Value & @.BitMask) <> 0
- If the result is non-zero, the Bit was set
To test if a particular bit is NOT SET
flip the bits of the value with NOT (~) and test that with the bitmask using
And (&)
Where (~Value & @.BitMask) <> 0
"Larry" wrote:

> Hi Everyone,
> I've using the query below to extract some information. ONe of the items
in
> the where clause is a check for an integer column for 0x1.
> Question: What is the correct syntax for checking this column? As below,
I
> have pm.ProductStatus = 0x1. This is returning rows. Am I correct in thi
s?
> Thanks in advance
> Larry
>
> SELECT am.AccountID, cpc.ProductID, cpc.ProductName, cpc.ManufacturerName,
> cpc.ManufacturerPartNumber, wtl.Description, cpws.Notes
> FROM
> Account_Master am
> INNER JOIN ClientProductCatalog cpc
> ON am.AccountID = cpc.AccountID
> INNER JOIN ProductMaster pm
> ON cpc.ProductID = pm.ProductID
> INNER JOIN ClientProductWorkflowStatus cpws
> ON cpc.ClientProductCatalogID = cpws.clientProductCatalogID
> INNER JOIN WorkflowStatusTypeLkp wfst
> ON cpws.WorkflowStatusTypeLkpID = wfst.WorkFlowStatusTypeLkpID
> INNER JOIN WorkflowTypeLkp wtl
> ON cpws.WorkflowTypeLkpID = wtl.WorkflowTypeLkpID
> WHERE wfst.WorkflowStatusTypeLkpID = 3 -- Needs more information from
> customer?
> AND cpc.Deactivated = 0 -- Product still active?
> AND pm.ProductStatus = 0x1 -- Awaiting status?|||You can use a simple integer one constant for this. Why do you want to
make the code proprietary, unreadable and unmaintainable?
There are many things we do not do in SQL and one of them is low-level,
bit and byte operations. That is what assembly languages and things
like C are for. The next error is writing a lot of flags into the
schema, to mimic an old punch card file system. For example,
"deactivated" looks like a flag which should be a more general status
code or an even date.
We also do not construct data element names like "WorkflowTypeLkpID",
since a thing cannot be both a type code and an identfier. Where is
the "ClientProducts" or perhaps "ClientProductWorkflow" table which
would have "ClientProductWorkflowStatus" in a column, since a status
code is not an entity?
You confuse data and metadata concepts. I would bet you are used to an
OO language with lots of low-level constructs, without even a year of
data modeling.|||Is it not the case that bit wise operators are part of the SQL 99
specification?
Thomas
"Larry" <Larry@.discussions.microsoft.com> wrote in message
news:0F8877FE-B2B3-416C-9803-C2501F1C60A5@.microsoft.com...
> Hi Everyone,
> I've using the query below to extract some information. ONe of the items
> in
> the where clause is a check for an integer column for 0x1.
> Question: What is the correct syntax for checking this column? As below,
> I
> have pm.ProductStatus = 0x1. This is returning rows. Am I correct in
> this?
> Thanks in advance
> Larry
>
> SELECT am.AccountID, cpc.ProductID, cpc.ProductName, cpc.ManufacturerName,
> cpc.ManufacturerPartNumber, wtl.Description, cpws.Notes
> FROM
> Account_Master am
> INNER JOIN ClientProductCatalog cpc
> ON am.AccountID = cpc.AccountID
> INNER JOIN ProductMaster pm
> ON cpc.ProductID = pm.ProductID
> INNER JOIN ClientProductWorkflowStatus cpws
> ON cpc.ClientProductCatalogID = cpws.clientProductCatalogID
> INNER JOIN WorkflowStatusTypeLkp wfst
> ON cpws.WorkflowStatusTypeLkpID = wfst.WorkFlowStatusTypeLkpID
> INNER JOIN WorkflowTypeLkp wtl
> ON cpws.WorkflowTypeLkpID = wtl.WorkflowTypeLkpID
> WHERE wfst.WorkflowStatusTypeLkpID = 3 -- Needs more information from
> customer?
> AND cpc.Deactivated = 0 -- Product still active?
> AND pm.ProductStatus = 0x1 -- Awaiting status?|||Politics. There were products that still had a low-level
implementation based on particular hardware. Did you look at the
SQL:2003 that deprecared bit data types? Did you look at the problems
in implementations in actual products?|||Only AND, OR, and NOT, and only for data of type BOOLEAN, are
in the SQL-99 standard as far as I know. I wouldn't expect the standard
to specify bitwise operations for numerical data, or any other operators
that behaved as though the data were represented in a particular way.
Steve Kass
Drew University
Thomas C wrote:

>Is it not the case that bit wise operators are part of the SQL 99
>specification?
>
>Thomas
>"Larry" <Larry@.discussions.microsoft.com> wrote in message
>news:0F8877FE-B2B3-416C-9803-C2501F1C60A5@.microsoft.com...
>
>
>

Monday, March 12, 2012

Qry: update person_course datetimestamp = 10/6/1900 12:00:01 AM fails!

I am trying to modify data in a SQL Server database using the query below. But it fails; why??

update person_course datetimestamp = '10/6/1900 12:00:01 AM' where personid > 470

I get
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'datetimestamp'.You need the key word SET

update person_course SET datetimestamp = '10/6/1900 12:00:01 AM' where personid > 470|||Thanks. I saw it after I had sent the message. Aaarrrgh!!! Long day I guess.

Wednesday, March 7, 2012

q; the owner of the table

Hello,
I need to user full name for the table as seen below.
SELECT @.RowCount = COUNT(*)
FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
T2 may be re-created by different users, how can I get this working for all
users if the creator is not dbo?JIM
You will have to identify an user and then creating dynamic sql if I
understood you properly.
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:295BC91E-3F82-4EB8-BFAF-71DCC335FAC5@.microsoft.com...
> Hello,
> I need to user full name for the table as seen below.
> SELECT @.RowCount = COUNT(*)
> FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
> T2 may be re-created by different users, how can I get this working for
> all
> users if the creator is not dbo?
>|||Thanks for the reply, I am trying to select the data whoever creates it, if
the owner is not dbo, my query is not working, owner may be many users since
people drop it an recreate it from time to time.
"Uri Dimant" wrote:

> JIM
> You will have to identify an user and then creating dynamic sql if I
> understood you properly.
>
>
>
> "JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
> news:295BC91E-3F82-4EB8-BFAF-71DCC335FAC5@.microsoft.com...
>
>|||JIM
So specify
CREATE TABLE user.TableName (col INT)
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:8EA06ACD-F357-4434-92B2-25B5A47006B3@.microsoft.com...[vbcol=seagreen]
> Thanks for the reply, I am trying to select the data whoever creates it,
> if
> the owner is not dbo, my query is not working, owner may be many users
> since
> people drop it an recreate it from time to time.
> "Uri Dimant" wrote:
>|||JIM.H. wrote:
> Thanks for the reply, I am trying to select the data whoever creates it, i
f
> the owner is not dbo, my query is not working, owner may be many users sin
ce
> people drop it an recreate it from time to time.
> "Uri Dimant" wrote:
Having a table that is repeatedly dropped/created by your users seems
like a bad idea. Why not create a permanent table, owned by dbo, and
have your users TRUNCATE it instead of dropping/creating it? Or better
yet, explain why the need to drop/create exists, and perhaps we can
offer a better idea?
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:8EA06ACD-F357-4434-92B2-25B5A47006B3@.microsoft.com...[vbcol=seagreen]
> Thanks for the reply, I am trying to select the data whoever creates it,
> if
> the owner is not dbo, my query is not working, owner may be many users
> since
> people drop it an recreate it from time to time.
> "Uri Dimant" wrote:
>|||Thanks for the reply. I agree however that is the part of the code I do not
have control over, I just need to figure out a way select it based on the
different owner.
"Tracy McKibben" wrote:

> JIM.H. wrote:
> Having a table that is repeatedly dropped/created by your users seems
> like a bad idea. Why not create a permanent table, owned by dbo, and
> have your users TRUNCATE it instead of dropping/creating it? Or better
> yet, explain why the need to drop/create exists, and perhaps we can
> offer a better idea?
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>

q; the owner of the table

Hello,
I need to user full name for the table as seen below.
SELECT @.RowCount = COUNT(*)
FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
T2 may be re-created by different users, how can I get this working for all
users if the creator is not dbo?JIM
You will have to identify an user and then creating dynamic sql if I
understood you properly.
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:295BC91E-3F82-4EB8-BFAF-71DCC335FAC5@.microsoft.com...
> Hello,
> I need to user full name for the table as seen below.
> SELECT @.RowCount = COUNT(*)
> FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
> T2 may be re-created by different users, how can I get this working for
> all
> users if the creator is not dbo?
>|||Thanks for the reply, I am trying to select the data whoever creates it, if
the owner is not dbo, my query is not working, owner may be many users since
people drop it an recreate it from time to time.
"Uri Dimant" wrote:
> JIM
> You will have to identify an user and then creating dynamic sql if I
> understood you properly.
>
>
>
> "JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
> news:295BC91E-3F82-4EB8-BFAF-71DCC335FAC5@.microsoft.com...
> > Hello,
> > I need to user full name for the table as seen below.
> >
> > SELECT @.RowCount = COUNT(*)
> > FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
> >
> > T2 may be re-created by different users, how can I get this working for
> > all
> > users if the creator is not dbo?
> >
>
>|||JIM
So specify
CREATE TABLE user.TableName (col INT)
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:8EA06ACD-F357-4434-92B2-25B5A47006B3@.microsoft.com...
> Thanks for the reply, I am trying to select the data whoever creates it,
> if
> the owner is not dbo, my query is not working, owner may be many users
> since
> people drop it an recreate it from time to time.
> "Uri Dimant" wrote:
>> JIM
>> You will have to identify an user and then creating dynamic sql if I
>> understood you properly.
>>
>>
>>
>> "JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
>> news:295BC91E-3F82-4EB8-BFAF-71DCC335FAC5@.microsoft.com...
>> > Hello,
>> > I need to user full name for the table as seen below.
>> >
>> > SELECT @.RowCount = COUNT(*)
>> > FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
>> >
>> > T2 may be re-created by different users, how can I get this working for
>> > all
>> > users if the creator is not dbo?
>> >
>>|||JIM.H. wrote:
> Thanks for the reply, I am trying to select the data whoever creates it, if
> the owner is not dbo, my query is not working, owner may be many users since
> people drop it an recreate it from time to time.
> "Uri Dimant" wrote:
Having a table that is repeatedly dropped/created by your users seems
like a bad idea. Why not create a permanent table, owned by dbo, and
have your users TRUNCATE it instead of dropping/creating it? Or better
yet, explain why the need to drop/create exists, and perhaps we can
offer a better idea?
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:8EA06ACD-F357-4434-92B2-25B5A47006B3@.microsoft.com...
> Thanks for the reply, I am trying to select the data whoever creates it,
> if
> the owner is not dbo, my query is not working, owner may be many users
> since
> people drop it an recreate it from time to time.
> "Uri Dimant" wrote:
>> JIM
>> You will have to identify an user and then creating dynamic sql if I
>> understood you properly.
>>
>>
>>
>> "JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
>> news:295BC91E-3F82-4EB8-BFAF-71DCC335FAC5@.microsoft.com...
>> > Hello,
>> > I need to user full name for the table as seen below.
>> >
>> > SELECT @.RowCount = COUNT(*)
>> > FROM T1 c INNER JOIN [MyInstance].MyDB.dbo.T2 b ON c.T1_ID=b.T2_ID
>> >
>> > T2 may be re-created by different users, how can I get this working for
>> > all
>> > users if the creator is not dbo?
>> >
>>|||Thanks for the reply. I agree however that is the part of the code I do not
have control over, I just need to figure out a way select it based on the
different owner.
"Tracy McKibben" wrote:
> JIM.H. wrote:
> > Thanks for the reply, I am trying to select the data whoever creates it, if
> > the owner is not dbo, my query is not working, owner may be many users since
> > people drop it an recreate it from time to time.
> >
> > "Uri Dimant" wrote:
> Having a table that is repeatedly dropped/created by your users seems
> like a bad idea. Why not create a permanent table, owned by dbo, and
> have your users TRUNCATE it instead of dropping/creating it? Or better
> yet, explain why the need to drop/create exists, and perhaps we can
> offer a better idea?
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>

Saturday, February 25, 2012

q; how to improve this transaction

Hello,
I have four different transactions such as below and I do one insert and one
update in each transaction and it seem it is slow and creates deadlock with
the user interface.
These transactions are performed against the tables that users are accessing
with another user interface. I have following two questions:
1. T2.TextField1 and TextField2 = @.TextField2 are Ok, Nok fields so I did
not put index since only two distinct values. Should I put indexes on these
fields?
2. Can I make this transaction let user interface do its task in case
accessing the same rows, I can start transaction again but I do not want
users get disturbed?
.
BEGIN TRANSACTION pTrans
BEGIN
INSERT INTO T1
(fields)
SELECT (fields)
FROM T2 INNER JOIN View1 ON T2.TrID = View1.MyTableID
WHERE (T2.TextField1 = @.TrType AND T2.TextField2 = @.TextField2)
UPDATE T2
SET TextField2 = 'Ok', TextField2Date=@.MyRunDateTime
FROM T2
WHERE (TextField1 = @.TrType AND TextField2 = @.TextField2)
IF @.@.ERROR <> 0
BEGIN
rollback transaction pTrans
return(-1)
END
ELSE
BEGIN
commit transaction pTrans
END
ENDadd a composite, non-clustered index on textfield1,textfield2.
otherwise, you will have to do a table scan for each update.
>|||On Wed, 12 Jul 2006 16:19:01 -0700, JIM.H.
<JIMH@.discussions.microsoft.com> wrote:
>I have four different transactions such as below and I do one insert and one
>update in each transaction and it seem it is slow and creates deadlock with
>the user interface.
>These transactions are performed against the tables that users are accessing
>with another user interface. I have following two questions:
>1. T2.TextField1 and TextField2 = @.TextField2 are Ok, Nok fields so I did
>not put index since only two distinct values. Should I put indexes on these
>fields?
>2. Can I make this transaction let user interface do its task in case
>accessing the same rows, I can start transaction again but I do not want
>users get disturbed?
How long does the transaction take if all alone on the machine?
How much IO does it do (from profiler or "set statistic io on" in
query analyzer)?
Are you familiar with the nolock hint?
Do you have access to the code for the other program accessing the
database? That's the one that might need the nolock.
You might want to do the selects for one or both statements (the
update does an implicit select) and put the results into temp files or
@.tables, then insert and update from them, to minimize locking times.
Depends on times and data volumes and PKs.
Josh

q; how to improve this transaction

Hello,
I have four different transactions such as below and I do one insert and one
update in each transaction and it seem it is slow and creates deadlock with
the user interface.
These transactions are performed against the tables that users are accessing
with another user interface. I have following two questions:
1. T2.TextField1 and TextField2 = @.TextField2 are Ok, Nok fields so I did
not put index since only two distinct values. Should I put indexes on these
fields?
2. Can I make this transaction let user interface do its task in case
accessing the same rows, I can start transaction again but I do not want
users get disturbed?
.
BEGIN TRANSACTION pTrans
BEGIN
INSERT INTO T1
(fields)
SELECT (fields)
FROM T2 INNER JOIN View1 ON T2.TrID = View1.MyTableID
WHERE (T2.TextField1 = @.TrType AND T2.TextField2 = @.TextField2)
UPDATE T2
SET TextField2 = 'Ok', TextField2Date=@.MyRunDateTime
FROM T2
WHERE (TextField1 = @.TrType AND TextField2 = @.TextField2)
IF @.@.ERROR <> 0
BEGIN
rollback transaction pTrans
return(-1)
END
ELSE
BEGIN
commit transaction pTrans
END
ENDadd a composite, non-clustered index on textfield1,textfield2.
otherwise, you will have to do a table scan for each update.
>|||On Wed, 12 Jul 2006 16:19:01 -0700, JIM.H.
<JIMH@.discussions.microsoft.com> wrote:
>I have four different transactions such as below and I do one insert and on
e
>update in each transaction and it seem it is slow and creates deadlock with
>the user interface.
>These transactions are performed against the tables that users are accessin
g
>with another user interface. I have following two questions:
>1. T2.TextField1 and TextField2 = @.TextField2 are Ok, Nok fields so I did
>not put index since only two distinct values. Should I put indexes on these
>fields?
>2. Can I make this transaction let user interface do its task in case
>accessing the same rows, I can start transaction again but I do not want
>users get disturbed?
How long does the transaction take if all alone on the machine?
How much IO does it do (from profiler or "set statistic io on" in
query analyzer)?
Are you familiar with the nolock hint?
Do you have access to the code for the other program accessing the
database? That's the one that might need the nolock.
You might want to do the selects for one or both statements (the
update does an implicit select) and put the results into temp files or
@.tables, then insert and update from them, to minimize locking times.
Depends on times and data volumes and PKs.
Josh