简体   繁体   English

SQL SERVER 2008存储过程未用CURSOR更新

[英]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] . 运行时,我可以在ctr.custom和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. 查询成功执行后,我正在尝试从CTR_Table中选择数据,并且在ctr.custom和ctr。[name]列中没有可用的值。 ( empty data ). (空数据)。 CTR_Table table contain GOOD values! CTR_Table表包含GOOD值!

What it`s wrong with my SP ? 我的SP有什么问题?

HERE you have my SP : 在这里,您有我的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 //对于每个酒店代码,我想更新CTR_Table表中的1行

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. 注意:我期望hotel_code属于一个表, hcode属于另一个表,以便这些列用作两个表的连接点。 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. 对于要在更新中使用的AnotherTableData上的一行,这两个字段必须相等。

The following query will update CTR_table without using cursors: 以下查询将CTR_table不使用游标的情况下更新CTR_table

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: 更新后,将填充CTR_table上的customname字段:

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 . 您可能需要研究MERGE语句 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. 表演将如火如荼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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