Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsql : Access query to TSQL union update conversion correct?

Tags:

union

t-sql

I have a query in access which i need to convert to a stored proc in sql server 2005.
the query in access is as follows:

UPDATE 
tblitem, 
tblFileSignature 
SET 
tblitem.strFileProcesstype = [tblFileSignature].[STRFILEPROCESSTYPE], tblitem.strFileSignatureType = [tblFileSignature].[strfilesignaturetype]
WHERE 
(((tblitem.strFileSignatureType) Is Null) AND 
((tblitem.strFileExclude)="n") AND 
((InStr([tblitem].[strfilesignature],[tblFileSignature].[strsignature]))=1) AND ((tblitem.uidItemType)=1 Or (tblitem.uidItemType)=5) AND 
((tblitem.uidCollection)=[forms]![frmSetup]![txtInputCol]) AND ((tblitem.strFileSignature) Not Like "d0c*") AND 
((tblFileSignature.strFileProcessType) Not Like "ZIP"));

in tsql.. would this be the same?

update tblItem 
set 
i.strFileProcesstype = f.strFileProcesstype,
i.strFileSignatureType = f.strfilesignaturetype 

from tblItem as I UNION tblFileSignature as F 

WHERE (((i.strFileSignatureType) Is Null) AND 
((i.strFileExclude)="n") AND 
((i.[strfilesignature] like F.strsignature)) AND 
((i.uidItemType)=1 Or 
(i.uidItemType)=5) AND 
((i.uidCollection)=@inputcolumn AND 
((i.strFileSignature) Not Like 'd0c%') AND 
((F.strFileProcessType) Not Like 'ZIP'));

thanks in advance

UPDATE:

so i'm going with the following. if i uncomment the declare and select clause and just execute from the declare down, it runs, if i comment the declare and select parts, it says error near ';'.

UPDATE I  
SET 
    I.strFileProcesstype = F.STRFILEPROCESSTYPE, 
    I.strFileSignatureType = F.strfilesignaturetype
--declare @uidcollectionID int
--select I.strFileSignatureType
from 
tblItem I
    inner join tblFileSignature F
on 
I.strfilesignature = left(F.strsignature,len(I.strfilesignature))

WHERE I.strFileSignatureType Is Null 
    AND I.strFileExclude='n' 
    AND I.uidItemType in (1,5) 
    AND I.uidCollection = @uidCollectionID
    AND left(I.strFileSignature,3) <> 'd0c' 
    AND F.strFileProcessType <> 'ZIP';

any ideas?

like image 661
phill Avatar asked Dec 06 '25 07:12

phill


2 Answers

You should change the

  • Double Quotes to Single Quotes
  • * to %
  • Replace the InStr with LIKE

Other than that, it looks fine to me.

like image 113
Lieven Keersmaekers Avatar answered Dec 08 '25 09:12

Lieven Keersmaekers


No, you'd use a JOIN, not a UNION.

You can either make it a CROSS JOIN, and continue to apply the join conditions in the WHERE clause, or you can make it an inner join:

from tblItem as I INNER JOIN tblFileSignature as F 
ON ((InStr(i.[strfilesignature],F.[strsignature]))=1)

And remove that condition from the WHERE clause (Lieven's answer also applies).

like image 31
Damien_The_Unbeliever Avatar answered Dec 08 '25 09:12

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!