SQL - Using a select statement and loop to populate a stored procedure

MG SQL Muppet

I have to use a stored procedure to update some fields. The sp looks like this:

exec dbo.spMaintainConditionComponent
@Mask = 16
,@ConditionComponentUID = 
   ,@Status = 1
   ,@ArchiveUser = ''
   ,@ArchiveDate = ''
   ,@ArchiveReason = 'Removed'
   ,@ArchiveTypeID = '464'

All of the parameters have the same value except the @ConditionComponentUID which can be obtained from this select code:

SELECT     tbConditionComponent.ConditionComponentUID
FROM         tbAsset INNER JOIN
                  tbConditionComponent ON tbAsset.AssetUID = tbConditionComponent.ParentID
WHERE     (tbAsset.Status = 1)

I also need to use a 'while loop' or 'cursor' as multiple records can't be updated at the same time. Please could someone assist?

Veera

There are many ways to update the data. The worst one is 'by using loop'. If you don't have any other choice Use Cursor to LOOP through values:

DECLARE LoopCursor CURSOR FOR
        SELECT tbConditionComponent.ConditionComponentUID
        FROM tbAsset INNER JOIN tbConditionComponent ON tbAsset.AssetUID = tbConditionComponent.ParentID
        WHERE (tbAsset.Status = 1)

DECLARE @ConditionComponentUIDTemp AS UNIQUEIDENTIFIER

    OPEN LoopCursor
    FETCH NEXT FROM LoopCursor INTO @ConditionComponentUIDTemp

    WHILE @@FETCH_STATUS = 0
    BEGIN
        exec dbo.spMaintainConditionComponent
        @Mask = 16
        ,@ConditionComponentUID = @ConditionComponentUIDTemp
        ,@Status = 1
        ,@ArchiveUser = ''
        ,@ArchiveDate = ''
        ,@ArchiveReason = 'Removed'
        ,@ArchiveTypeID = '464'

        FETCH NEXT FROM LoopCursor INTO @ConditionComponentUIDTemp
    END

CLOSE LoopCursor
DEALLOCATE LoopCursor

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to convert this select statement into a stored procedure?

분류에서Dev

Simple If Statement in Stored Procedure

분류에서Dev

Loop the select query result set in MYSQL Stored Procedure

분류에서Dev

Delete statement not working in Stored Procedure

분류에서Dev

Nested stored procedure in SQL

분류에서Dev

running a stored proc from a select statement with or without loop

분류에서Dev

Stored procedure with select where in array

분류에서Dev

Using Group By in a SqlDataSource with a stored procedure

분류에서Dev

Determine if a SQL Insert/Update statement affects the result from a stored Select Statement

분류에서Dev

how to split a xml format data into row column format in sql server 2008 using stored procedure

분류에서Dev

Call stored procedure from PL/SQL Job

분류에서Dev

SQL Stored Procedure Get Distinct and Update

분류에서Dev

Converting Stored Procedure into a query (SQL Server Compact)?

분류에서Dev

PL/SQL Stored Procedure - IF THEN ELSE condition

분류에서Dev

Varchar value not passing into SQL Server stored procedure

분류에서Dev

SQL stored procedure: how to concatenate parameter value?

분류에서Dev

SQL server create stored procedure syntax error

분류에서Dev

how to secure a valuable stored procedure in sql server

분류에서Dev

SQL Stored procedure does not enter IF and CASE condition

분류에서Dev

SQL SERVER Select MAX Procedure

분류에서Dev

PHP mysqli prepared statement for stored procedure with out parameter

분류에서Dev

Why regular expression not working in sql server select statement using like

분류에서Dev

Failing to populate the Options in Select element using javascript

분류에서Dev

T - SQL advanced SELECT Statement

분류에서Dev

SQL server stored procedure issue in via with c#

분류에서Dev

SQL Server : stored procedure many-to-many table query

분류에서Dev

How to pass schema as parameter to a stored procedure in sql server?

분류에서Dev

Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

분류에서Dev

SQL Server: EXECUTE AS clause of stored procedure not granting sysadmin permissions

Related 관련 기사

  1. 1

    How to convert this select statement into a stored procedure?

  2. 2

    Simple If Statement in Stored Procedure

  3. 3

    Loop the select query result set in MYSQL Stored Procedure

  4. 4

    Delete statement not working in Stored Procedure

  5. 5

    Nested stored procedure in SQL

  6. 6

    running a stored proc from a select statement with or without loop

  7. 7

    Stored procedure with select where in array

  8. 8

    Using Group By in a SqlDataSource with a stored procedure

  9. 9

    Determine if a SQL Insert/Update statement affects the result from a stored Select Statement

  10. 10

    how to split a xml format data into row column format in sql server 2008 using stored procedure

  11. 11

    Call stored procedure from PL/SQL Job

  12. 12

    SQL Stored Procedure Get Distinct and Update

  13. 13

    Converting Stored Procedure into a query (SQL Server Compact)?

  14. 14

    PL/SQL Stored Procedure - IF THEN ELSE condition

  15. 15

    Varchar value not passing into SQL Server stored procedure

  16. 16

    SQL stored procedure: how to concatenate parameter value?

  17. 17

    SQL server create stored procedure syntax error

  18. 18

    how to secure a valuable stored procedure in sql server

  19. 19

    SQL Stored procedure does not enter IF and CASE condition

  20. 20

    SQL SERVER Select MAX Procedure

  21. 21

    PHP mysqli prepared statement for stored procedure with out parameter

  22. 22

    Why regular expression not working in sql server select statement using like

  23. 23

    Failing to populate the Options in Select element using javascript

  24. 24

    T - SQL advanced SELECT Statement

  25. 25

    SQL server stored procedure issue in via with c#

  26. 26

    SQL Server : stored procedure many-to-many table query

  27. 27

    How to pass schema as parameter to a stored procedure in sql server?

  28. 28

    Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

  29. 29

    SQL Server: EXECUTE AS clause of stored procedure not granting sysadmin permissions

뜨겁다태그

보관