Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure Update with Condition

Author  Topic 

zero1de
Posting Yak Master

105 Posts

Posted - 2012-05-04 : 05:25:32
The update should process only those records that were not processed.
The two fields are here flag for the update.
- Archived=1 process and 0 not processed
- Stapelnummer= 'TIFF Printer'

Here is the procedure: it works but not correctly. It cross all records include i.Fordnr = P.Fordnr!

How can I determine in the beginning when starting the update process that only those records that have not yet been archived will be updated ?. I did it with IF exists condition but does not try? Does anyone have another idea?

IF EXISTS (SELECT FROM archives WHERE archived immo is null and Stapelnummer = 'Tiff_Printer')
BEGIN
UPDATE I
SET NAME = P.NAME1
, FIRST NAME = P.NAME2
, ENGAGEMENTNR = P.ENGAGEMENT
, FORDERGNR = P.FORDERGNR
, Partner ID = P.PARTNERID
, Archived = 1
FROM immo I
INNER JOIN ON i.Fordnr TBL_IMMO_PARTNER P = P.Fordnr
where i.archiviert where <> = 1 and i.stapelnummer='Tiff_Printer'
END
ELSE
BEGIN
RAISERROR ('No document found!', 16, 1)
END

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-04 : 06:57:31
quote:
Originally posted by zero1de

The update should process only those records that were not processed.
The two fields are here flag for the update.
- Archived=1 process and 0 not processed
- Stapelnummer= 'TIFF Printer'

Here is the procedure: it works but not correctly. It cross all records include i.Fordnr = P.Fordnr!

How can I determine in the beginning when starting the update process that only those records that have not yet been archived will be updated ?. I did it with IF exists condition but does not try? Does anyone have another idea?

IF EXISTS (SELECT FROM archives WHERE archived immo is null and Stapelnummer = 'Tiff_Printer')
BEGIN
UPDATE I
SET NAME = P.NAME1
, FIRST NAME = P.NAME2
, ENGAGEMENTNR = P.ENGAGEMENT
, FORDERGNR = P.FORDERGNR
, Partner ID = P.PARTNERID
, Archived = 1
FROM immo I
INNER JOIN ON i.Fordnr TBL_IMMO_PARTNER P = P.Fordnr
where i.archiviert where <> = 1 and i.stapelnummer='Tiff_Printer'
END
ELSE
BEGIN
RAISERROR ('No document found!', 16, 1)
END

Assuming you are using Microsoft SQL Server, I am surprised that it even runs - should be giving you lot of syntax errors. Based on my understanding of what you described the following should work. RED shows changes I made to correct syntax errors and green shows change I would suggest to account for presence of nulls
    UPDATE I
SET NAME = P.NAME1,
[FIRST NAME] = P.NAME2,
ENGAGEMENTNR = P.ENGAGEMENT,
FORDERGNR = P.FORDERGNR,
[PARTNER ID] = P.PARTNERID,
Archived = 1
FROM immo I
INNER JOIN TBL_IMMO_PARTNER P
ON i.Fordnr TBL_IMMO_PARTNER P = P.Fordnr
WHERE ISNULL(i.archiviert,0) WHERE <> = 1
AND i.stapelnummer = 'Tiff_Printer'
Go to Top of Page

zero1de
Posting Yak Master

105 Posts

Posted - 2012-05-06 : 08:36:21
THX it works :)

quote:
Originally posted by sunitabeck

quote:
Originally posted by zero1de

The update should process only those records that were not processed.
The two fields are here flag for the update.
- Archived=1 process and 0 not processed
- Stapelnummer= 'TIFF Printer'

Here is the procedure: it works but not correctly. It cross all records include i.Fordnr = P.Fordnr!

How can I determine in the beginning when starting the update process that only those records that have not yet been archived will be updated ?. I did it with IF exists condition but does not try? Does anyone have another idea?

IF EXISTS (SELECT FROM archives WHERE archived immo is null and Stapelnummer = 'Tiff_Printer')
BEGIN
UPDATE I
SET NAME = P.NAME1
, FIRST NAME = P.NAME2
, ENGAGEMENTNR = P.ENGAGEMENT
, FORDERGNR = P.FORDERGNR
, Partner ID = P.PARTNERID
, Archived = 1
FROM immo I
INNER JOIN ON i.Fordnr TBL_IMMO_PARTNER P = P.Fordnr
where i.archiviert where <> = 1 and i.stapelnummer='Tiff_Printer'
END
ELSE
BEGIN
RAISERROR ('No document found!', 16, 1)
END

Assuming you are using Microsoft SQL Server, I am surprised that it even runs - should be giving you lot of syntax errors. Based on my understanding of what you described the following should work. RED shows changes I made to correct syntax errors and green shows change I would suggest to account for presence of nulls
    UPDATE I
SET NAME = P.NAME1,
[FIRST NAME] = P.NAME2,
ENGAGEMENTNR = P.ENGAGEMENT,
FORDERGNR = P.FORDERGNR,
[PARTNER ID] = P.PARTNERID,
Archived = 1
FROM immo I
INNER JOIN TBL_IMMO_PARTNER P
ON i.Fordnr TBL_IMMO_PARTNER P = P.Fordnr
WHERE ISNULL(i.archiviert,0) WHERE <> = 1
AND i.stapelnummer = 'Tiff_Printer'


Go to Top of Page
   

- Advertisement -