简体   繁体   中英

SQL SERVER 2008 Stored procedure NOT update with CURSOR

I dont understand what's happening with my stored procedure when I want to execute it. while it runs I can see values in ctr.custom and ctr.[name] . AFTER The query executed successfully I'm tryin to SELECT data from CTR_Table and there NO available values in ctr.custom and ctr.[name] columns. ( empty data ). CTR_Table table contain GOOD values!

What it`s wrong with my SP ?

HERE you have my SP :

ALTER proc [dbo].[AddOrUpdateData]
as

DECLARE  @idP int;
SET @idP = 10; //always has a valid value

DECLARE @hCodePvd int;

//get all hotel codes
DECLARE item CURSOR FOR SELECT hotel_code FROM  AnotherTableData ;

OPEN item ;

FETCH NEXT FROM item  INTO @hCodePvd ;

//for each hotel code I want to update 1 row in CTR_Table table

WHILE @@FETCH_STATUS = 0
BEGIN 


            UPDATE ctr SET              
                ctr.custom=r1.ccode_provider,
                ctr.[name]=r1.cname_provider                
            FROM
                CTR_Table ctr  INNER JOIN   AnotherTableData r1 ON ctr.[Standard] = r1.Code
            WHERE  r1.hcode =@hCodePvd AND ctr.IDP = @idP 


    FETCH NEXT FROM item  INTO @hCodePvd ;
END

CLOSE item ;
DEALLOCATE item ;

Based on the code, here's what I understand what the tables involved might look like with sample data:

--CTR_table:
idP         standard   custom     name
----------- ---------- ---------- ----------
10          Standard1  NULL       NULL
10          Standard2  NULL       NULL
10          Standard3  NULL       NULL
10          Standard4  NULL       NULL
10          Standard5  NULL       NULL

--AnotherTableData:
hotel_code  hcode       code       ccode_provider cname_provider
----------- ----------- ---------- -------------- --------------
1           1           Standard1  ccode1         cname1
2           2           Standard2  ccode2         cname2
3           3           Standard3  ccode3         cname3
4           4           Standard4  ccode4         cname4
5           5           Standard5  ccode5         cname5

Note: I was expecting that hotel_code belonged to one table and hcode belonged to the other table so that these columns serve as a join point for the two tables. However, the code shows that both these fields belong to the same table. The two fields have to be equal for a row on AnotherTableData to be used in the update.

The following query will update CTR_table without using cursors:

DECLARE  @idP int;
SET @idP = 10; --always has a valid value

UPDATE CTR_table
SET
    CTR_table.custom = AnotherTableData.ccode_provider,
    CTR_table.[name] = AnotherTableData.cname_provider
FROM CTR_table
INNER JOIN AnotherTableData
    ON CTR_table.idP = @idp
    AND CTR_table.standard = AnotherTableData.code
    AND AnotherTableData.hotel_code = AnotherTableData.hcode

After the update, the custom and name fields on CTR_table will be populated:

SELECT * FROM CTR_table

idP         standard   custom     name
----------- ---------- ---------- ----------
10          Standard1  ccode1     cname1
10          Standard2  ccode2     cname2
10          Standard3  ccode3     cname3
10          Standard4  ccode4     cname4
10          Standard5  ccode5     cname5

You may want to look into the MERGE statement . It's a tad complicated, but you'll be able to run typical "update, add of not exists" scenario's in a single query.

Performance will go through the roof.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM