I need to ensure that the parameter "lastname" that is being passed to my Oracle procedure is in "initcap" format. Can I use the INITCAP function while accepting the value itself?
create or replace PROCEDURE SP_SPRIDEN_7_INSERT
(
PIDM in varchar2,
STUD_ID IN OUT VARCHAR2,
INITCAP(LASTNAME) IN VARCHAR2,
ERROR OUT VARCHAR2
)
You can't define a procedure like this. The easiest way to get this behavior is to explicitly call initcap on that argument before using it in the procedure's body.
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 am attempting to create a table valued parameter for input into an MS SQL Server stored proc. My create statement:
CREATE TYPE dbo.tvt_AbusedBy AS TABLE
( Assessment_Behavorial_AbusedID int, Assessment_BehavorialID int,
Ref_Abuse_TypeID int, Abuser_Name varchar(50), GenderID int,
Relationship_TypeID int, Age int)
When attempting to add as a parameter into the proc:
CREATE PROCEDURE [dbo].[qryAddUpdateMultipletblAssessment_Behavorial_Abused]
#prm_Assessment_Abused AS dbo.tvt_AbusedBy READONLY,
I get an error reading "The parameter #prm_Assessment_Abused cannot be declared READONLY since it is not a table valued parameter".
It does not seem to recognize it as a table valued parameter.
If I remote the READONLY stipulation, it gives me an error "Parameter or variable #prm_Assessment_Abused has an invalid type.
Must be an issue with how I am attempting to create the table valued parameter type.
Any ideas?
Don't use AS when adding parameters in a stored procedure declaration:
CREATE PROCEDURE [dbo].[qryAddUpdateMultipletblAssessment_Behavorial_Abused]
#prm_Assessment_Abused dbo.tvt_AbusedBy READONLY
From MS documentation - https://learn.microsoft.com/en-us/sql/t-sql/statements/create-procedure-transact-sql?view=sql-server-2017
You don't need AS before parameters, use it afters parameters declaration.
Example from link (a little edited):
CREATE PROCEDURE dbo.usp_add_kitchen
#dept_id int, #kitchen_count int NOT NULL
AS
BEGIN
UPDATE dbo.Departments
SET kitchen_count = ISNULL(kitchen_count, 0) + #kitchen_count
WHERE id = #dept_id
END;
GO
I have a Stored Procedure, see this picture .
I want to modify Department parameter as nvarchar(30), where it is currently as nvarchar(10).
So how could I achieve this?
Thank you so much
declare #department nvarchar(30)
set #department = CAST (#department_in as nvarchar(30))
If you right click on it, you have the option 'modify' which will generate an alter procedure script for you.
Change the parameter to an nvarchar(30) and run the alter script.
You can't change the type of variable if you already dclared it. In other words:
to change the type of variable, yo need to declare it once again, and as this thread states, you cannot do it.
It is possible to query database objects according to their parameter names, types, length etc using the information schema view
[INFORMATION_SCHEMA].[PARAMETERS]
But you cannot simply update the length on some tables to modify the length, etc of a procedure parameter.
All these objects should be updated using
ALTER PROCEDURE ...
command, at least for stored procedures
You can get the source codes of a procedure by running sp_helptext as follows
sp_helptext yourStoredProcedureName
Then modify the parameter and execute the script as ALTER command
Hey guys I want to use SQL Server function,
I never use function in SQL Server and I only know that function must return a value so I have two stored procedure (1 for insert and 1 for select)
My stored procedures look like this
For insert:
create proc INS_tblteststud
#stdtid int=null,
#name varchar(50) =null,
#fullname varchar(50)=null,
#address varchar(50)=null,
#city varchar(50)=null,
#country varchar(50)=null,
#contno varchar(50)=null
as
begin
insert into tbl_student_test(name,fullname,address,city,country,contno)
values
(#name,#fullname,#address,#city,#country,#contno)
end
And for select:
Create proc SEL_tblteststud
as
begin
select * from tbl_student_test
end
Now I want to know, how can I convert these statements(Insert, Select) into functions? And which is better to use stored procedure or function?
You are mixing procedure and function...
A procedure is meant for doing something
A function is meant for reading only
It is not allowed to place data changing commands within a function.
What you are trying is just not possible...
UPDATE Some insight about functions and procedures
There are three types of functions
scalar functions: Return one scalar value and are very bad performing
multi statement table valued functions (with BEGIN ... END): Return a resultset, but are bad performing too
inline table valued functions: They are great, but restricted. Think of them like of a VIEW with pre-compiled parameters
A procedure has a return value too, which is of tpye INT and is not used to return the SP's result, but kind of execution status.
A SP might just do something and return no procedure result at all. If you want to return procedural results you must either use
output parameters
or you must call a SELECT within your SP. It is possible to grab the result of an internal SELECT from outside, but this is very cumbersome...
Is it possible to pass datatable as parameter into stored procedure ?
So, something like
exec MyStoredProcedure #MyDataTable
I am using SQL SERVER 2008.
You need to create User-defined Table Type first.
-- Create the data type
CREATE TYPE udtt_Table AS TABLE
(
Column1 int,
Column2 varchar(10),
Column3 datetime
)
GO
You can use user-defined table type in your stored procedure like the following,
CREATE PROCEDURE usp_User
(
#UserTable udtt_Table READONLY
)
...
....
You could create your own type: https://msdn.microsoft.com/en-us/library/ms175007.aspx
But it's quite a work... What exactly do you want to reach?
EDIT: Another suggestion
Use an XML-Parameter (see comments below)