I have a Sybase procedure returning nothing. Now I want to modify the procedure and wanted an OUT parameter similar to SYS_REFCURSOR in Oracle to get the result of a query. Can you please suggest ?
Code:
create procedure get_rec_test
#buss_date datetime
as
begin
INSERT INTO TEST
SELECT * FROM short_positions_report
end
GO
In the above code how to add the out parameter to get resultset from TEST table. ? Am a novice in sybase and any help will be highly appreciated.
Related
I am trying to get System Datetime for a column when a new row is inserted or updated into a table using stored Proc in MS SQL. How can I achieve it?
I have tried below code
CREATE PROCEDUCE test_Cl_INSERT
#SRC_ID int,
#CREATED_BY datatime
AS
BEGIN
INSERT into dbo.CL_Batch(SRC_ID, Created_BY)
VALUES(#SRC_ID, CURRENT_TIMESTAMP)
END
EXEC dbo.test_Cl_INSERT
#SRC_ID=44
ERROR : #CREATED_BY parameter missing
This will work:
CREATE PROCEDURE test_Cl_INSERT
#SRC_ID int
AS
BEGIN
INSERT into dbo.CL_Batch(SRC_ID, Created_BY)
VALUES(#SRC_ID, CURRENT_TIMESTAMP)
END
EXEC dbo.test_Cl_INSERT
#SRC_ID=44
Your procedure signature is:
CREATE PROCEDUCE test_Cl_INSERT
#SRC_ID int,
#CREATED_BY datatime
You attempt to execute as:
EXEC dbo.test_Cl_INSERT #SRC_ID=44
Do you see something missing? You should. Your procedure has 2 parameters but you provide only 1 when you attempt to execute it. That is your problem. As already noted, you don't use that paramter within the logic of the procedure so why does it exist at all?
You must execute your procedure like this:
EXEC dbo.test_Cl_INSERT #SRC_ID=44, #CREATED_BY = '20201124 12:49';
Notice I just assigned a random value to the parameter since it (the parameter) is not used within your procedure code. That solves the question you ask. However, you have more important issues to consider.
I know I am an idiot.. But I have been trying to create this simple stored procedure in my Oracle database for some time now and I keep getting the error "procedure created with compilation errors". I can't seem to find anything wrong with it and I am following the syntax I have found online. I am using an Oracle xe 11g server with pl/sql 11. Please help!
CREATE OR REPLACE PROCEDURE hr.countEmployee(passin IN NUMBER)
IS
BEGIN
SELECT COUNT(*) FROM hr.mitch_employee_motors WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
END;
Aleksej hit the nail on the head and I follow up with the code that reflects his comment.
CREATE OR REPLACE PROCEDURE hr.countemployee (passin IN NUMBER)
IS
l_count INTEGER;
BEGIN
SELECT COUNT (*) INTO l_count
FROM hr.mitch_employee_motors
WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
DBMS_OUTPUT.PUT_LINE (l_count);
END;
What are you using the write and execute your SQL and PL/SQL? I encourage you to try out SQL Developer. It would have helped you a whole lot in diagnosing the issue.
You can try:
CREATE OR REPLACE PROCEDURE hr.countEmployee(passin IN NUMBER,OUT_CURSOR OUT sys_refcursor)
IS
BEGIN
OPEN OUT_CURSOR FOR
SELECT COUNT(*) FROM hr.mitch_employee_motors WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
END;
I've seen in Oracle SQL that there is something called SYS_REFCURSOR which is used in a SP to get a recordset from a table
I want to do the same thing in SQL Server so I can a SP to get a recordset of a specific table. I've searched a lot looking for similar solutions but all what I've seen are all related to Oracle SQL.
Any suggestions please?
P.S. To be more clear, what do I want is just using an SP to select all records instead of the normal query.
i.e. instead of using:
SELECT * FROM EMP_DEPARTMENT
use SP to do the same thing.
Assuming you mean stored procedure with SP you can to this :
create procedure dbo.spMyProcedure as
begin
set nocount on
select * from emp_department
end;
than you can do this :
exec dbo.spMyProcedure
Is this what you mean ?
In SQL Server, you can simply encapsulated the SELECT query in a stored procedure (using * only as an example since an explicit column list is the best practice):
CREATE PROC dbo.GetEmployeeDepartments
AS
SELECT * FROM EMP_DEPARTMENT;
GO
The SQL Server client API will stream the result as a fast-forwared read-only resultset, where individual rows can be used.
I have procedure dbo.GetData:
Create procedure dbo.GetData
As
Begin
Select * from dbo.tblName
End
And I also created a schema [ABC], table ABC.tblName
So, I would like to change schema [dbo] of table in procedure dbo.GetData into [ABC] by using another stored procedure.
And, the result is:
Create procedure dbo.GetData
As
Begin
Select * from [ABC].tblName
End
How can I do it?
Thank you everyone.
I'm not sure I understand what you're asking, but I think you simply want to change the code being executed in the stored procedure. If so, a simple ALTER PROCEDURE would do the trick to change the code, but not the name:
ALTER PROCEDURE dbo.GetData
AS
BEGIN
SELECT * FROM [ABC].tblName
END
Full syntax of [ALTER PROCEDURE] 1 (for SQL Server)
If this is not what you're after, please clarify the question.
Update:
The only real solution I see is that you script out your procs, and then use a text-editor to replace the dbo. values with [ABC]. values.
I just attempted to try and do this through updating the system tables, but in SQL Server 2012 (which I use), it simply gets far too complex for that.
Try this hope this may help you!
ALTER SCHEMA NewSchemaName TRANSFER OldSchemaName.ObjectName
I have a stored procedure with the following format :
CREATE PROCEDURE proc_test
#variable1 int
AS
SELECT column1, column2, ....
FROM table_test
Because this procedure doesn't return any values explicitly, it's difficult to know how many lines of results have been returned. What's more, I cannot modify this stored procedure since it's created by another programmer and already used in the application.
So my question is, if I want to get the resultset generated by the select statement, how can I do it ?
After fired the procedure you can try use a session variable ##rowcount:
select ##rowcount
I'm not sure, maybe it will help you.