how can I count the row number in a virtual table that is temporarily created in the stored procedure - sql

I have stored procedure like this
create store procedure onetimeprocessing
as
begin
declare #input_data (id int,title varchar(400),topic varchar(400))
insert into #input_data
select id,title,topic from dB
I just want to count number of records in the virtual table #input_data
How should I get the count.Please help me
Thanks in Advance

select count(*) from #input_data
or after the insert
select ##rowcount

Why not just have this?
create store procedure onetimeprocessing
as
begin
select COUNT(*) as TheCount from dB
Edit
I'm having difficulty understanding why other folk would suggest to load #inputdata first, then count from that. or return an extra result set with ##ROWCOUNT...

Its available in ##ROWCOUNT:
declare #input_data table (id int,title varchar(400),topic varchar(400))
insert into #input_data select ....
SELECT ##ROWCOUNT

Related

How to call first SQL Server stored procedure to second stored procedure

I have three tables:
TB1 : P (p_id (pk),p_name,p_description)
TB2: PV (pv_id (pk),p_id(fk),v_id(fk),cost)
TB3: V (V_id (pk),name)
I have used below SQL Server stored procedure "Addp" which inserts p_name and p_description on being called.
CREATE PROCEDURE AddP
(#name CHAR(50),
#description VARCHAR(255))
AS
BEGIN
DECLARE #PCount INT
SELECT #PCount = Count(P_ID)
FROM P
WHERE P_Name= #name
IF #PCount = 0
BEGIN
INSERT INTO P(P_Name, P_Description)
VALUES(#name, #description)
END
RETURN ##IDENTITY
END
Now, I would like to create another procedure where I can insert "v_id" and cost along with "Addp" stored procedure.My second SP looks like following. My question is How should I call my first stored procedure into second new stored procedure. So that when I execute my second stored procedure I should be able to insert p_name,p_description,vi,cost everything in one shot?
CREATE PROCEDURE AddPWithV (#vi int, #cost decimal(12,4))
AS
BEGIN
INSERT INTO PV (V_ID, Cost)
VALUES(#vi, #cost)
Return ##IDENTITY
END;
I am new to stored procedure.So,please guide me through explanation.
Any help is appreciated!
Thanks in advance!
Just declare twor more variables
Shown as below:
DECLARE #_vid INT
DECLARE #_cost numeric(18,2) // as per your column daya type
Select #_vid =vid from TB3 where name =#name ;
Select #_cost = cost from TB2 where
P_id in (select p_id from TB1 where name =#name );
Now you have both values you can insert where ever you want ..

how to insert cases into a table based on a parameter without duplicating the main select query?

I have a insert statement in sql server 2008 like below:
INSERT INTO MYTABLE
SELECT Id,Name,Date
FROM #temp
...joins...
where ...conditions...
I need to add a parameter to the code, and based on the parameter I will decide which table the output data should be inserted into. Check below:
declare #checkValue bit;
if (#checkValue = 1)
begin
INSERT INTO MYTABLE
SELECT Id,Name,Date
FROM #temp
...joins...
where ...conditions...
end
else
begin
INSERT INTO MY_OTHER_TABLE
SELECT Id,Name,Date
FROM #temp
...joins...
where ...conditions...
end
I handled the situation like above for now, however the main query is pretty long and I do not want to duplicate it, it does not look professional to me. I am looking for a better way to deal with such an issue. Any help or advice should be appreciated. Thanks.
You could use dynamic SQL (evil :), like so:
declare #cmd varchar(max) = '
INSERT INTO ' + #tableName + '
SELECT ...
';
execute (#cmd);
Or you create stored procedures (one statically INSERTs to table A, another one into table B >> code duplication) which are called depending on #checkValue, or a combination of both like here:
Stored procedure to insert values into dynamic table
create SP:
CREATE PROCEDURE ExampleSelect
AS
SELECT Id,Name,Date
FROM #temp
...joins...
where ...conditions...
GO
use insert..exec to insert data:
declare #checkValue bit;
if (#checkValue = 1)
begin
INSERT INTO MYTABLE
EXEC ExampleSelect
end
else
begin
INSERT INTO MY_OTHER_TABLE
EXEC ExampleSelect
end

Iterate through values of a table variable in a stored procedure

I am writing a stored proc as follows:
CREATE PROCEDURE ListByOrderRequestId
#EntityId int,
#EntityTypeId varchar(8)
AS
BEGIN
SET NOCOUNT ON;
Declare #IDS table(OrderRequestId int)
INSERT INTO #IDS
SELECT OrderRequestTaskId
FROM OrderRequestTask ORT WITH (NOLOCK)
WHERE ORT.OrderRequestId = #EntityId
SELECT N.EntityNoteId AS Id,
N.UpdateDate AS DateStamp,
N.EntityNoteText,
N.EntityId,
N.EntityNoteTypeId,
N.EntityTypeId
FROM EntityNote N
WHERE (N.EntityId = #EntityId AND N.EntityTypeId ='ORDREQ')
*OR(N.EntityId = #IDS VAL1 AND N.EntityTypeId ='TASK')
OR(N.EntityId = #IDS VAL2 AND N.EntityTypeId ='TASK')*
END
The table #IDS can have 0 or 1 or more values in it. I want to loop through the values in #TDS and create the where clause above accordingly. Please help me.
As opposed to looping through the table, you could just use it in your where clause like this:
Select * From {Your Table} Where ID in (Select OrderRequestId From IDS)
This is much faster than looping.

Using variables in stored procedures

I am using Microsoft SQL server and writing a stored procedure that contains many select statements. Here I want two variables that hold results of two select statements, and I want to add those two variables to get final result. Please help me doing this (syntax and example).
Below is the syntax for SQL Server:
DECLARE #UserEmail Varchar(250)
DECLARE #LoginID INT
SET #UserEmail = 'a#b.org'
select #LoginID = LoginID from Login L
Where L.UserEmail = #UserEmail
You should clarify which DB you are using. In MS SQL Server, you can use temporary table variable like this:
BEGIN
SELECT product_id,product_name INTO #temp1 FROM products;
SELECT product_id,product_name INTO #temp2 FROM products;
SELECT * FROM #temp1
UNION
SELECT * FROM #temp2;
END
There are several types of temporary table variable in MS SQL Server.I've used one of them.To know more about this, just search "MS SQL Server Temporary tables" in web.
EDIT:
Here is another example with another type of temporary table variable in MS SQL Server.
DECLARE #temp1 TABLE (product_id INT,product_name VARCHAR(100));
DECLARE #temp2 TABLE (product_id INT,product_name VARCHAR(100));
INSERT INTO #temp1 SELECT product_id,product_name FROM products WHERE cat_id=1;
INSERT INTO #temp2 SELECT product_id,product_name FROM products WHERE cat_id=2;
SELECT product_id,product_name
FROM #temp1
UNION
SELECT product_id,product_name
FROM #temp2;

Loop in SQL query

Basically I want to extract id from a table and insert that id in another table with some other data. So the algorythm goes:
foreach id in (select id from table)
{
insert into table2 values (id,etc,etc);
}
How to perform this function with an SQL query?
Use
insert into table2
select id, etc, etc from table
instead of for loop.
Here's a helpful article.
insert into table2 select id,etc,etc from table;
The syntax to use is a SQL statement with a select and insert into combination.
Select Into
http://www.w3schools.com/sql/sql_select_into.asp
In SQL Query if you want to use looping then use cursor Pls. check below sample
--DECLARE CURSOR
DECLARE #ID VARCHAR(MAX)
--READ DATA FROM TABLE TO CURSOR
DECLARE IDCR CURSOR FOR SELECT id FROM table
OPEN IDCR;
FETCH NEXT FROM IDCR INTO #ID
WHILE ##FETCH_STATUS = 0
BEGIN
INSERT INTO table2 VALUES (#ID,etc,etc)
FETCH NEXT FROM IDCR INTO #ID
END
CLOSE IDCR;
DEALLOCATE IDCR;
Please check below ref. link https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-cursor-transact-sql