Execute stored procedure in UPDATE T-SQL - sql

I have next t-sql code
UPDATE
#f_contr_temp
SET
sum_percent = (EXEC get_f_contr_credit_delay_pz fct.f_contr, #date_loop, #sum_percent OUTPUT)
FROM
#f_contr_temp AS fct,
f_garanty AS fg
WHERE
fct.f_contr = fg.f_contr
Im trying to update sum_percent column but throw an exception what i can't execute procedure in update.
I have no idea, how update my column. Please help.

Use a temporary table to store result of your S.P. as follow:
CREATE TABLE #temp (percentage decimal(18,9))
INSERT INTO #temp
(EXEC get_f_contr_credit_delay_pz fct.f_contr, #date_loop, #sum_percent OUTPUT)
UPDATE
#f_contr_temp
SET
sum_percent = (select top 1 percentage from #temp)
FROM
#f_contr_temp AS fct,
f_garanty AS fg
WHERE
fct.f_contr = fg.f_contr
I suppose your S.P. return values, so I use TOP 1 command to get only the first.
Pay attention fct.f_contr, which is its source? In this way the query is not correct.

Related

How to Update a column Before Select?

I am setting a stored procedure for select and I want to update the value of one column in the database Before doing the Select.
This is what I tried but it's not working.
#roleID int and #query varchar(240)
SELECT
EP.Equipe_Projet_Id AS PROJET_ID,
U.USR_ID,
CleRepartition = CASE
WHEN #RoleID = 1 AND #query IS NOT NULL
THEN 100
AND (UPDATE EQUIPE_PROJET SET CleRepartition = 100
WHERE EP.Equipe_Projet_Id = #PROJET_ID AND EP.Role_Id = 3)
ELSE NULL
END
FROM
[EQUIPE_PROJET] EP
Expecting update of column on database and having it's value
i want to update the value of Column CleRepartition in the database while selecting.
This is not possible in a SELECT query. A SELECT retrieves data from the database. An UPDATE modifies data. These are two separate statements and cannot be combined.
You are doing this work in a stored procedure. Within a stored procedure, you can run an UPDATE and SELECT in any order, so you can accomplish both tasks. If you are concerned about data changing in the database between the two statements, you can wrap them in a transaction.
Stored procedure can do update then select after.
So I added the query of update in the beginning, then I do the select.
Like this:
UPDATE EP
SET CleRepartition = CASE
WHEN #RoleID = 1 AND #query IS NOT NULL
THEN 100
AND (UPDATE EQUIPE_PROJET
SET CleRepartition = 100
WHERE EP.Equipe_Projet_Id = #PROJET_ID
AND EP.Role_Id = 3)
ELSE NULL
END
FROM [EQUIPE_PROJET] EP
SELECT
EP.Equipe_Projet_Id AS PROJET_ID,
U.USR_ID,
CleRepartition
FROM
[EQUIPE_PROJET] EP
I hope that this will help someone.

Stored Procedure that will raise if there's no data in SSRS report

I'm looking for a way to create a stored procedure that will raise if there's no data from a report that I have in SSRS. Thanks.
For your dataset query, put the results in a temp table.
SELECT ...
INTO #TEMP_DATA
FROM ....
Put the count of the table in a variable.
DECLARE #COUNT AS INTEGER = (SELECT COUNT(*) FROM #TEMP_DATA)
If variable = 0 then run your SP.
IF #COUNT = 0 THEN
SP_MY_STORED_PROC
SELECT your results for the report query.
SELECT * FROM #TEMP_DATA
OR Rerun your query to populate the table with refreshed data first (if that's what the SP does).
IF #COUNT = 0 THEN
SELECT ...
INTO #TEMP_DATA
FROM ....
If i understand you correctly. If you don't have a data for your query in SSRS. You can use 'NoRowsMessage' option in your Tablix properties.

Need to wait on specific column state change in SQL

I need to check for the status change of a certain column in the table
I can do it using while loop, where i can get the column value and check the value and break from the loop if value is changed.
i am using SQL server 2008.
is there a better way?
Here is the sample sql query
declare #status int = 1
select #status = status from MyTable with (nolock) where Id = 100034
while #status <> 3
begin
WAITFOR DELAY '00:01'
select #status = status from MyTable with (nolock) where Id = 100034
end
Have you considered to use a trigger instead of an stored procedure? This is exactly, what are triggers for.
CREATE TRIGGER reactOnStatus3
ON MyTable
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
IF Status = 3
EXEC DoTheMagicStoredProcedure;
END;

How to update 1 column only but to all rows with a calculation?

I want to update 1 column only for all rows in my table with the total calculation of :
The data of column multiply with random number between 0.9 to 1.2..
UPDATE TABLE
SET ABC = (SELECT ABC*(RAND()*((1.2-0.9)+0.9)) FROM TABLE)
Well idk how to write it in a proper T-SQL. I didn't get the expected result in SQL Server 2008 R2 with my query. Need your help guys to solve this things up.
Or you can give me another solution with using cursor.
Think you have a problem of parenthesis (for the rand "range"), than the way to write the query (if I understood well)
update table
set abc = abc * (RAND() * (1.2-0.9) + 0.9)
CAUTION
the "random" multiplicator will be the same for all the rows updated by this statement, as noticed by Damien_The_Unbeliever
SET NOCOUNT ON
DECLARE
#ABC_ID AS INT,
#RANDOM_GEN_NO AS VARCHAR(50),
#TEMP AS VARCHAR(50)
DECLARE ABC_CURSOR CURSOR FOR
SELECT #ABC_ID, RANDOM_GEN_NO FROM ABCTABLE FOR UPDATE OF RANDOM_GEN_NO
OPEN ABC_CURSOR
FETCH NEXT FROM ABC_CURSOR
INTO #EMP_ID, #RANDOM_GEN_NO
WHILE (##FETCH_STATUS = 0)
BEGIN
SELECT #TEMP = ABC*(RAND()*((1.2-0.9)+0.9))
UPDATE ABCTABLE SET ABC = #TEMP WHERE CURRENT OF ABC_CURSOR
FETCH NEXT FROM ABC_CURSOR
INTO #ABC_ID, #RANDOM_GEN_NO
END
CLOSE ABC_CURSOR
DEALLOCATE ABC_CURSOR
SET NOCOUNT OFF
OR IN Update Statement update ABCTable SET ABC = abc * yourcode
no need of select in update statement
UPDATE YourTABLE
SET ABC = ABC*(RAND()*((1.2-0.9)+0.9))
ok from your code block I see several basic syntax errors. The basic syntax of an update statement is
UPDATE [Tablename]
SET [Columnname] = (Value or Calculation or subquery)
WHERE (condition)
For more information check this
Try the below code:
UPDATE A
SET A.ABC = B.rand
from table A
inner join
(SELECT A.pkcolumn,A.ABC*(RAND()*((1.2-0.9)+0.9)) as rand FROM TABLE A)B
on A.pkcolumn = B.pkcolumn

How can one iterate over stored procedure results from within another stored procedure....without cursors?

I'm not sure if this is something I should do in T-SQL or not, and I'm pretty sure using the word 'iterate' was wrong in this context, since you should never iterate anything in sql. It should be a set based operation, correct? Anyway, here's the scenario:
I have a stored proc that returns many uniqueidentifiers (single column results). These ids are the primary keys of records in a another table. I need to set a flag on all the corresponding records in that table.
How do I do this without the use of cursors? Should be an easy one for you sql gurus!
This may not be the most efficient, but I would create a temp table to hold the results of the stored proc and then use that in a join against the target table. For example:
CREATE TABLE #t (uniqueid int)
INSERT INTO #t EXEC p_YourStoredProc
UPDATE TargetTable
SET a.FlagColumn = 1
FROM TargetTable a JOIN #t b
ON a.uniqueid = b.uniqueid
DROP TABLE #t
You could also change your stored proc to a user-defined function that returns a table with your uniqueidentifiers. You can joing directly to the UDF and treat it like a table which avoids having to create the extra temp table explicitly. Also, you can pass parameters into the function as you're calling it, making this a very flexible solution.
CREATE FUNCTION dbo.udfGetUniqueIDs
()
RETURNS TABLE
AS
RETURN
(
SELECT uniqueid FROM dbo.SomeWhere
)
GO
UPDATE dbo.TargetTable
SET a.FlagColumn = 1
FROM dbo.TargetTable a INNER JOIN dbo.udfGetUniqueIDs() b
ON a.uniqueid = b.uniqueid
Edit:
This will work on SQL Server 2000 and up...
Insert the results of the stored proc into a temporary table and join this to the table you want to update:
INSERT INTO #WorkTable
EXEC usp_WorkResults
UPDATE DataTable
SET Flag = Whatever
FROM DataTable
INNER JOIN #WorkTable
ON DataTable.Ket = #WorkTable.Key
If you upgrade to SQL 2008 then you can pass table parameters I believe. Otherwise, you're stuck with a global temporary table or creating a permanent table that includes a column for some sort of process ID to identify which call to the stored procedure is relevant.
How much room do you have in changing the stored procedure that generates the IDs? You could add code in there to handle it or have a parameter that lets you optionally flag the rows when it is called.
Use temporary tables or a table variable (you are using SS2005).
Although, that's not nest-able - if a stored proc uses that method then you can't dumpt that output into a temp table.
An ugly solution would be to have your procedure return the "next" id each time it is called by using the other table (or some flag on the existing table) to filter out the rows that it has already returned
You can use a temp table or table variable with an additional column:
DECLARE #MyTable TABLE (
Column1 uniqueidentifer,
...,
Checked bit
)
INSERT INTO #MyTable
SELECT [...], 0 FROM MyTable WHERE [...]
DECLARE #Continue bit
SET #Continue = 1
WHILE (#Continue)
BEGIN
SELECT #var1 = Column1,
#var2 = Column2,
...
FROM #MyTable
WHERE Checked = 1
IF #var1 IS NULL
SET #Continue = 0
ELSE
BEGIN
...
UPDATE #MyTable SET Checked = 1 WHERE Column1 = #var1
END
END
Edit: Actually, in your situation a join will be better; the code above is a cursorless iteration, which is overkill for your situation.