How to Create Stored Procedures in SQL server 2008 - sql-server-2008

Select tblppmp.idn
,tblppmp.total_item as a_total
,tblRequest.Quantity as b_total
,tblppmp.total_item - tblRequest.Quantity as itemsleft
FROM ppmp.dbo.tblppmp
INNER JOIN
(SELECT
tblrequest.idn
,sum(tblRequest.Quantity) AS Quantity
FROM ppmp.dbo.tblrequest
WHERE tblrequest.dr_year = 2015
GROUP BY tblrequest.idn) tblrequest ON tblppmp.idn = tblrequest.idn
Above is my code, how to create stored procedure and the value of the dr_year may change depending on the textbox or combobox selected by the user..sample is where tblrequest.dr_year = "Textbox1.text"

Whether the different value comes from a textbox or a combobox is irrelevant. It will always come into your stored procedure as a SQL parameter.
To create the Stored Procedure, simply execute the following on your database:
USE AdventureWorks
GO
CREATE PROCEDURE dbo.uspGetAddress
AS
SELECT * FROM Person.Address
GO
Replace the database name "AdventureWorks" in the USE command and the procedure name "uspGetAddress" in the CERATE PROCEDURE command with your database and procedure names respectively
Since you have a dynamic date value, you'll need to add the parameter. This changes your SQL code to look something more like this:
CREATE PROCEDURE dbo.uspGetAddress #Date datetime
AS
...
GO
Check out MsSQLTips for more info

Related

How to write a procedure in Mysql with table name passed as parameter

I want to write a MySQL procedure. It should take a parameter, which is table name.
I have two tables with similar schema and I want to perform similar operations on them. I should run a select query which is a self join and then load results into cursor and do something based on that.
CREATE PROCEDURE MY_PROCEDURE (table_name CHAR(15)
BEGIN
//
DECLARE A CURSOR HERE, WRITE AN INNER JOIN QUERY AND LOAD results into cursor
//
.......
.......
.......
END
Now I need to call the Procedure
CALL MY_PROCEDURE('First_table');
CALL MY_PROCEDURE('Second_table');
My goal is to re-use code here.

MySQl IF NOT EXISTS, CREATE Stored procedure

I come from a background with Microsoft SQl using their Server management studio. I have recently switched to mysql and am looking to create a stored procedure with the same method I use with MSSQL. I want to create a procedure if it does not exists because I prefer that to dropping if exists. Below is the syntax I would use in MSSQL. Any help would be much appreciated.
IF NOT EXISTS
(
SELECT
1
FROM
sysobjects WITH (NOLOCK)
WHERE
[type] = 'P' AND name = 'Sproc'
)
EXEC('CREATE PROCEDURE dbo.Sproc AS BEGIN SET NOCOUNT ON; END')
GO
ALTER PROCEDURE dbo.Sproc
(
#Sproc_Params
)
AS
BEGIN
.... --Sproc code
END
You can look in mysql.proc to see if a procedure exists.
SELECT db, name FROM mysql.proc WHERE db = 'dbo' AND name = 'Sproc';
However, in MySQL, you cannot ALTER PROCEDURE to replace the procedure body. You can only change a few attributes of the procedure. See http://dev.mysql.com/doc/refman/5.7/en/alter-procedure.html for details.
So you will have to drop and create the procedure anyway, if you want to change its parameters or its body.

MySQL Stored Procedures: Using a result set returned from a stored procedure in another stored procedure with MYSQL 5.6

In our MySql database there are 3 stored procedures Sp1(), Sp2() and Sp3() . There is logic in Sp3() which I want to use in the stored procedures Sp1() and Sp2(), I know that using a temporary table in Sp3() and using that temporary table in Sp1() and Sp2() works.
To save time and memory it is preferred not to create a temporary table.
One extra piece of info is I can return a result set from a stored procedure to my client. But I am unable to make this store procedure (SP3) work like a subquery, where the result set is returned from the Stored procedure Sp3() and can be compared with another table in Sp2() and Sp1() based on a id key.
I want to try something like below, which can be used in stored procedures Sp1() and Sp2():
Assuming Sp3() returns an id field along with other fields:
Select
id
from
EmployeeTable e
where 5000 >
(Select salary from (call Sp3()) where id= e.id);

Querying on the stored procedure result set

Im using SQL Server 2008. I have a result set from the stored procedure and i want to fire some more queries on the result set for e.g order by desc /asc and some querying. So what is the best way to do it. Using views or by using OPENQUERY.
Please help.
You can save results of stored procedure calls in any table or table variable that has same number and type of fields as the result set returned by the stored procedure, for example:
CREATE TABLE #temp(col1 INT, col2 VARCHAR(10))
INSERT INTO #temp(col1, col2)
EXEC usp_Proc1(#param1)
SELECT *
FROM #temp
Condition is that usp_Proc1 returns rows consisting of columns of type INT and VARCHAR(10) (in that order).

SQL Server 2008 Multi-db stored procedure: The multi-part identifier could not be bound

I am trying to write a stored procedure for SQL Server 2008. It is for a migration tool that migrates data from one database to another.
I want to get all records from the old database that aren't yet present in the new database. For that end I use the stored procedure below. But I get the error
The multi-part indentifier could not
be bound
while executing the stored procedure. What can I do to make this work?
Here is the SP:
SELECT *
FROM Measurement_device
WHERE Measurement_device.Department_Code = '99'
AND mir.dbo.Gages.Gage_code != Device_Number
It's because you have the mir.dbo.Gages.Gage_code condition when that table is not referenced in the FROM clause. Try something like this:
SELECT m.*
FROM Measurement_device m
LEFT JOIN mir.dbo.Gages g ON g.Gage_code = m.Device_Number
WHERE m.Department_Code = '99'
AND g.Gage_code IS NULL -- where the gage_code/device_number does not already exist in mir database