SQL Server stored procedure with default values - sql-server

I Have SQL Server stored procedure with ~100 parameters, with default values. I have to pass only 3 of them. Is it possible to use StoredProcedureQuery? Now if I have procedure:
create procedure MyProc(#Param1 varchar(30) = "test", #Param2 smallint = 1, #Param3 char = 'a', #Param4 char = 'b',
#Param5 varchar(50) = "test", #Param6 tinyint = 7, #Param7 varchar(3) = "tes", #Param8 varchar(3) = "ADS",
#Param9 varchar(50) = "TEST", #Param10 varchar(8) = "TEST", #Param11 varchar(222) = "TEST")
and executing procedure like this:
StoredProcedureQuery storedProcedureQuery = entityManagerFactory.createEntityManager()
.createStoredProcedureQuery("MyProc");
query.registerStoredProcedureParameter("Param9", String.class, ParameterMode.IN)
.setParameter("Param9", "test9");
query.registerStoredProcedureParameter("Param10", Character.class, ParameterMode.IN)
.setParameter("Param10", "test10");
query.registerStoredProcedureParameter("Param11", String.class, ParameterMode.IN)
.setParameter("Param11", "test11");
Unfortenatly value "test9" goes to #Param1, "test10" goes to #Param2 and "test11" goes to #Param3.
Is it possible to pass parameters by name, not by position?

It is possible to pass parameters by name using JPA and SQL Server using NativeQuery.
Query query = entityManagerFactory.createEntityManager().createNativeQuery("""
DECLARE #return_value int
EXEC #return_value = [dbo].[MyProc]
#Param9 = :Param9,
#Param10 = :Param10,
#Param11 = :Param11
""");
query.setParameter("Param9", "test9");
query.setParameter("Param10", "test10");
query.setParameter("Param11", "test11");

Related

EF 6 optional parameter stored procedure with output parameter

I am calling a stored procedure with optional params with an output parameter like below. I have tried ctx.Database.SqlQuery<string> and ExecuteSqlCommand. Both throws an error.
The stored procedure has these params with most of them being optional:
#Debug CHAR(1) = 'N',
#Yr VARCHAR(4),
#Collection_ID INT = NULL,
#SortOrder VARCHAR(100) = ' ',
#SrchDates VARCHAR(100) = '',
#FixedFormat CHAR(1) = 'C',
#ExtrctStrtFg CHAR(1) = 'D',
#APP_ID INT = NULL,
#MatchFound CHAR(1) = 'N' OUTPUT
My call is:
var retVal = frlentities.Database.SqlQuery<string>("exec pr_storedproc #Yr = 2000,#ExtrctStrtFg='F', #APP_ID=1234, #MatchFound OUTPUT;");
This complains that the fourth parameter #sortorder is provided as an output parameter. The same works when I do not have an output parameter and the I am getting a list with other stored procedures. Hope there is some way around this. I can provide all parameter by importing the stored procedure. But some stored procedures have too many parameters. Thank you

Bug in MVC Core 2 - Stored Procedures returns null

how do I report a bug to MVC Core2 team, the stored procedure Output Parameter returns null,
Here is my code
var p = new SqlParameter
{
ParameterName = "token",
DbType = System.Data.DbType.String,
Size = 100, Value = "Sarah",
Direction = System.Data.ParameterDirection.Output
};
var z = p.SqlValue;
var resp = await _context.Database.ExecuteSqlCommandAsync("exec dbo.yourstoredprocedure {0}, {1}","test", p);
var y = p.SqlValue;
var x = p.Value;
resp is always -1 and x and y are both null, the stored procedure is simply
create PROCEDURE [dbo].[yourstoredprocedure]
( #varone nvarchar(max),
#vartwo nvarchar(50) OUTPUT)
AS
insert into test (two, three) select #varone, #vartwo'
SET #vartwo = scope_identity()
return #vartwo;
Like I said in the comments, your SP is full of Syntax errors. It should probably look more like this:
CREATE PROCEDURE [dbo].[yourstoredprocedure] #varone nvarchar(max),
#vartwo nvarchar(50),
#varfour int OUTPUT AS --What happened to #varthree ?
INSERT INTO test (two, three)
VALUES (#varone, #vartwo);
SET #varfour = SCOPE_IDENTITY();
GO

Option of Sending parameters to stored procedure in SQL Server

I have a stored procedure that accepts 10 parameters. I want to send some of these parameters when executing another stored procedure. How can I do that?
Stored procedure definition:
create PROCEDURE [dbo].[SP_name]
#p1 int = null,
#p2 int = null
..
..
#p10 int = null
Execute statement :
exec SP_name valueForP2,vlaueForP5..;
Are there any why to tell exec statement to take just some of the variables?
You can specify the parameter names in the call to the procedure, the other parameters will use the default values.
create PROCEDURE [dbo].[SP_name]
#p1 int = null,
#p2 int = NULL,
#p10 int = NULL
AS
SELECT #p1, #p2, #p10
GO
-- Execute using named parameters
EXEC [SP_name] #p1 = 1, #p10 = 5;

Procedure with update new information

So I have been working on simple procedure for school that asks me to:
(Create a stored procedure called UpdateProduct that takes in a required Product ID parameter and optionally any other one or more fields in the Product table. Then the proc will update any fields passed in but leave any other fields as they were.
If executed like this: UpdateProduct #productID = 1, #name = ‘Steel Ball Bearing’; Only the name should change, all other fields should still contain the value there before the procedure
was called)
This is what I have but still I cant seem to get it to work properly. I am going nuts because I have been trying to figure this out for several days. Any help pointing out my mistake would be awesome.
USE AdventureWorks2012
GO
CREATE PROC UpdateProduct2
#ProductID INT ,
#Name nvarchar (50)= ISNULL,
#ProductNumber nvarchar (25) =ISNULL,
#Color nvarchar (15)=ISNULL
AS
BEGIN
UPDATE [Production].[Product]
SET
Name = ISNULL (#Name,Name),
ProductNumber = ISNULL (#ProductNumber,ProductNumber),
Color = ISNULL (#Color, Color)
WHERE #Name=Name
END
I think your where clause should be:
WHERE ProductID = #ProductID;
rather than
WHERE #Name=Name
Also you need to use NULL rather than ISNULL to set the parameter defaults:
CREATE PROC UpdateProduct2
#ProductID int,
#Name nvarchar(50) = NULL,
#ProductNumber nvarchar(25) = NULL,
#Color nvarchar(15) = NULL
AS
BEGIN
UPDATE [Production].[Product]
SET Name = ISNULL (#Name,Name),
ProductNumber = ISNULL (#ProductNumber,ProductNumber),
Color = ISNULL (#Color, Color)
WHERE ProductID = #ProductID;
END
EDIT
To answer the question about specifying NULL as the default, yes there is a reason, it allows you to call the procedure without passing the parameter.
Take the following two procedures:
CREATE PROCEDURE dbo.P1 #p1 VARCHAR(20), #p2 VARCHAR(20)
AS
BEGIN
SELECT P1 = #p1, P2 = #p2;
END;
GO
CREATE PROCEDURE dbo.P2 #p1 VARCHAR(20) = NULL, #p2 VARCHAR(20) = NULL
AS
BEGIN
SELECT P1 = #p1, P2 = #p2;
END;
The first using no default, and the latter with NULL as the default. The only way to call the first procedure is to send all parameters, e.g.
EXECUTE dbo.p1;
EXECUTE dbo.p1 #P2 = 'TEST';
EXECUTE dbo.p1 #P1 = 'TEST';
Will generate the following errors:
Msg 201, Level 16, State 4, Procedure P1, Line 0
Procedure or function 'P1' expects parameter '#p1', which was not supplied.
Msg 201, Level 16, State 4, Procedure P1, Line 0
Procedure or function 'P1' expects parameter '#p1', which was not supplied.
Msg 201, Level 16, State 4, Procedure P1, Line 0
Procedure or function 'P1' expects parameter '#p2', which was not supplied.
Whereas this:
EXECUTE dbo.p2;
EXECUTE dbo.p2 #P2 = 'TEST';
EXECUTE dbo.p2 #P1 = 'TEST';
Will generate:
P1 P2
NULL NULL
P1 P2
NULL TEST
P1 P2
TEST NULL

How to pass udtt into a stored procedure in SQL Server Management Studio

I have a SP prc_Foo_Delete which has the following signature:
ALTER PROCEDURE [prc_Foo_Delete]
#fooIds [int_udtt] READONLY,
#deleteReason int,
#comment nvarchar(512),
#deletedBy nvarchar(128)
int_udtt is define as:
CREATE TYPE [int_udtt] AS TABLE(
[Id] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
I tried to call this SP in Management Studio with following script:
DECLARE #return_value int
EXEC #return_value = [prc_Foo_Delete]
#fooIds = 3,
#deleteReason = 2,
#comment = N'asfdasdf',
#deletedBy = N'asdfa'
SELECT 'Return Value' = #return_value
GO
The error I got is: Operand type clash: int is incompatible with int_udtt. How do I pass in a int or a list of int to call in this tool (I know how to do it in code but not in Management Studio).
Since you've defined your user defined type as a parameter on the stored procedure, you need to use that user-defined type, too, when calling the stored procedure! You cannot just send in a single INT instead....
Try something like this:
-- define an instance of your user-defined table type
DECLARE #IDs [int_udtt]
-- fill some values into that table
INSERT INTO #IDs VALUES(3), (5), (17), (42)
-- call your stored proc
DECLARE #return_value int
EXEC #return_value = [prc_Foo_Delete]
#fooIds = #IDs, -- pass in that UDT table type here!
#deleteReason = 2,
#comment = N'asfdasdf',
#deletedBy = N'asdfa'
SELECT 'Return Value' = #return_value
GO

Resources