Stored Procedure - sql

I am using SQl-server 2012 and this code is inside in a Stored Procedure.
These are my SQL queries.I want to execute "Insert" query when "Update" query not executed.
If Update query executed,then insert query should not be executed.
Update tblStock Set Balance= Balance + #ReduceRawQty
Where LocCode=#LocCode
AND ItemCode=#rawitemcode
and CostPrice=#rawcostprice
Insert Into tblStock(LocCode, ItemCode, CostPrice, Balance,Transfer,PCode)
Values(#LocCode,#rawitemcode,#RawCostPrice,#RawQty,0,#PCode)
Is there any method to do this ,please help me

You can check ##ROWCOUNT to see how many rows were affected by the UPDATE statement:
UPDATE ...
IF ##ROWCOUNT = 0
INSERT ...
Alternatively you can use EXISTS to check for a row first and decide which operation to perform:
IF EXISTS (SELECT 1 FROM tblStock WHERE LocCode=#LocCode AND ItemCode=#rawitemcode and CostPrice=#rawcostprice)
UPDATE ...
ELSE
INSERT ...

Related

How to get row count of select statement inside stored procedure in SQL Server

I have requirement where I want get row count of select statement inside stored procedure and if row count is equal to 0 then only it will execute the next statement
select A, B, C
from AT
where B = 1
If the above statement returns any rows, then it will not execute further but if this statement do not have any row then it will execute next statement. I have tried it using in two ways
##rowcount - it's not working properly
Using temp table by inserting select statement into table getting row count of table but using temp table is not optimize way
Is there any solution?
You could use IF NOT EXISTS:
IF NOT EXISTS (select A,B,C from AT where B=1)
BEGIN
-- sth
END
is there any solution like getting into variable without hitting to database again and again
DECLARE #my_rowcount INT;
select A,B,C from AT where B=1;
SET #my_rowcount = ##ROWCOUNT; -- immediately after select get ##ROWCOUNT
...
IF #my_rowcount = 0
BEGIN
-- sth
END
EDIT:
##ROWCOUNT Globle variable for database so it may return wrong Value if any other select statement processed in other sp in same databe
Nope. ##ROWCOUNT:
Returns the number of rows affected by the last statement.
You could easily check it with your SSMS(open 2 tabs, select 1 and 2 rows on each of them and then get ##ROWCOUNT respectively).

Execute stored procedure in UPDATE T-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.

DB2 SQL - Cannot successfully insert into table after trigger has been made

I have been running into problems inserting values into a table after having a trigger attached to it.
The trigger is the following:
CREATE TRIGGER trig
AFTER INSERT ON Follows
REFERENCING NEW as N
FOR EACH row
WHEN ((Select email from Celebrity) <> N.followed_email)
UPDATE followSuggestion SET followSuggestion.Suggested_follower = N.followed_email, followSuggestion.Suggested_followee = N.follower_email
;
the insert code is the following:
INSERT INTO follows
VALUES('Michael_Phelps#uss.net','Michael_Phelps#uss.net');
And the error is as follows
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES
INTO statement is more than one row. SQLSTATE=21000
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO statement is more than one row.
Thank you in advanced!
I think this is the problem:
(Select email from Celebrity) <> N.followed_email
The subquery will probably return more than one row so the scalar operator <> will not work.
You will have to rephrase that condition to something like:
WHEN ((Select COUNT(email) from Celebrity WHERE email = N.followed_email) = 0) ...

SQL Server - does trigger affects ##Rowcount?

I have a query which do UPSERT or update if exists and insert if not:
update MyTable
set [Name]=#NewValue
where ID=#ID
If ##RowCount = 0
insert into MyTable([Name])
values(#Name)
Now, I wonder if the ##RowCount will be affected by a query executed in a trigger? Let us say in my trigger I have:
insert into MyLogs(Description)
values("Some description...")
If the update is successful in my first query, the trigger will run the insert to MyLogs which will have affected rows.
##ROWCOUNT is tied to the scope of your current execution and is therefore unaffected by a trigger, which would run in a different scope.

Sql update statement, any way of knowing what it actually did?

Typically, I test an update by running a query using the where statement and then after verifying that it does what I think I want it to copying the where clause to the update statement and executing it. But is there any way of getting the statement to return what the update did besides the '4 rows updated'
Sure, take a look at the output clause of T-SQL
http://msdn.microsoft.com/en-us/library/ms177564.aspx
You could load your records into a temp table/variable in SQL Server:
DECLARE #Temp TABLE(ID INT)
INSERT INTO #Temp (ID)
SELECT ID
FROM Customer
WHERE AcctBalance > 5000
--inspect as needed
UPDATE Customer
SET AcctBalance = 0
WHERE ID IN (SELECT ID FROM #Temp)
That depend in the server, library that you use, in php, pdo exec return number of row effected by delete or update cluase