Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute result of select statement

How can I execute the results of my select query. The query below gives me some SQL statements back as result. I want to execute does statements, how to do this? All this is executed in SQL Sever Management Studio.

Query:

SELECT 'UPDATE Rolecopy SET PartofFT = ''' + R2.PlayedbyOT + ''', OriginalOT = ''' + R.PlayedbyOT + ''' WHERE RoleNo = ' + CAST(R.RoleNo AS VARCHAR) + CHAR(13)
FROM Role R INNER JOIN Role R2
ON R.PartofFT = R2.PartofFT AND R.RoleNo <> R2.RoleNo
WHERE EXISTS (
    SELECT PG.RoleNo
    FROM V_PurposeGrouping PG
    WHERE R.PartofFT = PG.PartofFT
    AND R.RoleNo <> PG.RoleNo
)

Result:

UPDATE Rolecopy SET PartofFT = 'Student', OriginalOT = 'Teacher' WHERE RoleNo = 5.00
UPDATE Rolecopy SET PartofFT = 'Project', OriginalOT = 'Teacher' WHERE RoleNo = 8.00
UPDATE Rolecopy SET PartofFT = 'Project', OriginalOT = 'description' WHERE RoleNo = 10.00
UPDATE Rolecopy SET PartofFT = 'Student', OriginalOT = 'Project' WHERE RoleNo = 15.0

0

like image 487
Creep Avatar asked May 17 '11 18:05

Creep


People also ask

What is the result of a SELECT statement?

The result of a SQL SELECT statement is a table.

When you execute a query for result using SQL SELECT statement is a?

Select Statement in SQL It is used to either fetch data according to some specified rule or display an entire table in SQL. The data displayed after the query execution is stored in a result table.

How do you run a SELECT statement?

You can execute a SELECT statement in the following two ways: If the SELECT statement returns only one row, use PREPARE and EXECUTE INTO. This type of SELECT is often called a singleton SELECT. If the SELECT statement returns more than one row, you must use cursor-management statements.

What is the execution order of a SELECT statement?

The first step in the process is the execution of the statements in From clause followed by the Join clause. The result of these operations is getting a cartesian product of our two tables. Once the From and Join were executed, the processor will get the qualified rows based on the condition On.


2 Answers

Try using your first query to open a cursor, then within the loop execute the result string as dynamic SQL.

declare commands cursor for
SELECT 'UPDATE Rolecopy SET PartofFT = ''' + R2.PlayedbyOT + ''', OriginalOT = ''' + R.PlayedbyOT + ''' WHERE RoleNo = ' + CAST(R.RoleNo AS VARCHAR) + CHAR(13)
FROM Role R INNER JOIN Role R2
ON R.PartofFT = R2.PartofFT AND R.RoleNo <> R2.RoleNo
WHERE EXISTS (
    SELECT PG.RoleNo
    FROM V_PurposeGrouping PG
    WHERE R.PartofFT = PG.PartofFT
    AND R.RoleNo <> PG.RoleNo
)

declare @cmd varchar(max)

open commands
fetch next from commands into @cmd
while @@FETCH_STATUS=0
begin
  exec(@cmd)
  fetch next from commands into @cmd
end

close commands
deallocate commands
like image 76
chezy525 Avatar answered Sep 23 '22 18:09

chezy525


Try using this :

SELECT 'UPDATE Rolecopy SET PartofFT = ''' + R2.PlayedbyOT + ''', OriginalOT = ''' + R.PlayedbyOT + ''' WHERE RoleNo = ' + CAST(R.RoleNo AS VARCHAR) + CHAR(13)
FROM Role R INNER JOIN Role R2
ON R.PartofFT = R2.PartofFT AND R.RoleNo <> R2.RoleNo
WHERE EXISTS (
    SELECT PG.RoleNo
    FROM V_PurposeGrouping PG
    WHERE R.PartofFT = PG.PartofFT
    AND R.RoleNo <> PG.RoleNo
)
FOR XML PATH ('')

For which the result is going into one string column:

UPDATE Rolecopy SET PartofFT = 'Student', OriginalOT = 'Teacher' WHERE RoleNo = 5.00
UPDATE Rolecopy SET PartofFT = 'Project', OriginalOT = 'Teacher' WHERE RoleNo = 8.00
UPDATE Rolecopy SET PartofFT = 'Project', OriginalOT = 'description' WHERE RoleNo = 10.00
UPDATE Rolecopy SET PartofFT = 'Student', OriginalOT = 'Project' WHERE RoleNo = 15.0

All in the same column.

like image 40
Shahab Avatar answered Sep 24 '22 18:09

Shahab