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

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

Related

Stored procedure cannot creates complex Type

This stored procedure when I add to Visual Studio EF database-first cannot create complex type in model browser. The stored procedure returns one column. Why can I not create a complex type?
I added SET FMTONLY OFF not working.
#id int,
#suay nvarchar(50),
#suyil nvarchar(50)
AS
BEGIN
DECLARE #deger int;
DECLARE #tablo TABLE (price float);
SET #deger = (SELECT price_second FROM harcamalar
WHERE id = #id AND suay = #suay AND suyil = #suyil);
INSERT INTO #tablo (price)
VALUES (#deger);
SELECT price FROM #tablo;
END

Column values as stored procedure input

I have the following code when inputting parameters for a SP:
USE [MDAmanager]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[spCougarExport]
#PropertyCodeString = NULL,
#Date = NULL,
#InferDate = NULL,
#TransactionCodeString = NULL
SELECT 'Return Value' = #return_value
GO
For the #PropertyCodeString I would like to pass the values from a seperate table with all the property codes.
Below is an example of the property codes
What I'm trying to achieve is have the SP always return all properties and not require updating the property codes when a new property is added.
Any assistance or direction would be greatly appreciated.
Assuming you are able to amend your SP then you can utilise a user defined table type e.g.
CREATE TYPE [dbo].[PropertyCode_UDT] AS TABLE
( [PropertyCode] [bigint] NOT NULL
)
GO
DECLARE #PCTab [dbo].[PropertyCode_UDT]
;
INSERT INTO #PCTab
( [PropertyCode]
)
SELECT PC.[PropertyCode]
FROM [dbo].[PropertyCode] PC
;
DECLARE #return_value int
EXEC #return_value = [dbo].[spCougarExport]
#PropertyCode = #PCTab,
#Date = NULL,
#InferDate = NULL,
#TransactionCodeString = NULL
;
Your Sp will need to be amended to accept the correct type for #PropertyCode and you can just join your query within the SP on this table to get all the Property Codes
Eg
CREATE PROCEDURE [dbo].[spCougarExport]
#Date [date]
, #InferDate [date]
, #TransactionCodeString [nvarchar] (MAX)
, #PropertyCode PropertyCode_UDT READONLY
AS BEGIN;
.......
SELECT ...
FROM #PropertyCode atPC

SQL stored procedure - table as parameter

I have a database with different tables (all the same structure) where I'd like to run a stored procedure having a parameter that defines which table to query.
I can't seem to figure it out:
CREATE SCHEMA test;
GO
First I created a schema
CREATE TYPE DataType as TABLE (
[datetime] [datetime] NULL,
[testVar] [bigint] NULL)
GO
Then I created the table type
USE [TestDataFiles]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [test].[testing]
(
-- Add the parameters for the stored procedure here
#datetime datetime,
#t DataType READONLY
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
select top(10) *
from #t
where [datetime] > #datetime
END
GO
Then I created the stored procedure.
Exec test.testing #t = 'table.1', #datetime = '2017-01-01'
However when I call it I get the following error:
Msg 206, Level 16, State 2, Procedure test, Line 0 [Batch Start Line 0]
Operand type clash: varchar is incompatible with DataType
Same happens with:
Exec test.testing #t = [table.1], #datetime = '2017-01-01'
I have seen an example where in the procedure between the begin and select you put something like:
INSERT INTO table.1
( datetime, testVar)
But table.1 (or table.2 etc as I have a list of tables) has data and I don't want to change it.
Unless I'm meant to create a dummy table like I did the TYPE?
The examples I've found online havent been useful.
To do that you will need to use dynamic SQL
The basic procedure is to build up a string that will hold the statement you will execute, then execute it
declare #SQL nvarchar(1000)
declare #t as nvarchar (1000)
set #t = 'MyTable'
set #Sql = 'Select * from ' + #t
exec sp_executesql #sql
You have to pass parameter of type DataType. So, create variable of that type and pass it into stored procedure like
declare #table1 DataType
INSERT INTO #table1(datetime, testVar) values (..., ...)
Exec test.testing #datetime = '2017-01-01', #t = #table1

SQL Server: stored proc tries to convert varchar OUTPUT into int #Return_Value

I have this stored procedure:
create proc Sponsors.GetLightBoxAd
(
#SponsorID varchar(30),
#ADIDOut varchar(30) OUTPUT,
#UserID varchar(30),
#ProjectID varchar(50),
#PlatformID int
)
as
begin
SELECT TOP 1 #ADIDOut = AD.ADID --my output. AD.ADID is a varchar(30) column
FROM Sponsors.AD
WHERE AD.Active = 1 and AD.SponsorID = #SponsorID
ORDER by NEWID() -- I want the first one picked at random
IF ISNULL(#ADIDOut,-1) != -1 --if the result set wasn't null, run some update queries with the result
BEGIN --These queries do not have output variables.
EXEC Sponsors.proc1 #ADIDOut, #SponsorID
EXEC Projects.proc2 #ProjectID,#ADIDOut,#UserID,#PlatformID
END --end the if
end --end the proc
go
This should return either a null value or a varchar.
However when I try to execute the query, SSMS auto-generates this code for me to run the query:
USE [MyDB]
GO
DECLARE #return_value int, --Why is this here???
#ADIDOut varchar(30) -- this should be my only output
EXEC #return_value = [Sponsors].[GetLightBoxAd]
#SponsorID = N'Alienware',
#ADIDOut = #ADIDOut OUTPUT,
#UserID = N'127.0.0.1',
#ProjectID = N'TestProject',
#PlatformID = 1
SELECT #ADIDOut as N'#ADIDOut'
SELECT 'Return Value' = #return_value --why is this here?
GO
And then it gives me an error because it appears to try to convert the result of ADIDOut into an int... Why is there an int #Return_Value variable and why is it trying to put my actual OUTPUT result into it?
The error was in this line:
IF ISNULL(#ADIDOut,-1) != -1
It seems since I put just -1 without quotes as the comparison, sql reads this as an integer and cannot compare it to a nullable string so the error is raised. Putting the -1's into quotes fixes the error and the query runs. Thanks for the help!

How to get data back from stored procedure

I'm trying to debug\execute a SP from t-sql (sql server 2008) and when I execute the following sql it returns only a message 'Command(s) completed successfully'.
I know the SP is returning the data in a table via the #RETURN_VALUE parameter, how do I get this to display in sql query window.
exec STARMS.dbo.GetMetaData ##Assembly=13366,##Namespace=NULL,##ParameterName=NULL,##Standard=0,##Timestamp=NULL
The SP looks something like this ( removed the main body) the interesting part is the creation of the temp table and then returning the contents of the temp table:
CREATE PROCEDURE [dbo].[GetMetaData]
##Assembly Sql_Variant = NULL,
##Namespace Varchar(30) = NULL,
##ParameterName Varchar(40) = NULL,
##Standard Bit = 0,
##Timestamp DateTime = NULL
AS
SET NOCOUNT ON
DECLARE #ResultTable TABLE (AssemblyId Int, Namespace Varchar(30), ParameterName Varchar(40), Value Varchar(100))
-- Does loads of stuff and then inserts into #ResultTable table...
SET NOCOUNT OFF
-- Return the result
SELECT AssemblyId, Namespace, ParameterName, [Value]
FROM #ResultTable
RETURN
Note: the reason I'm asking the question is because I'm trying to look at the performance of the SP in SQL Server 2008 and I'm unsure why this doesn't return the data in the query window.
Check your full code of SP, may be somewhere there is a RETURN statement, so - the SP will not return any data (pay attention on IF SomeContition IS TRUE RETURN):
CREATE PROCEDURE [dbo].[GetMetaData]
##Assembly Sql_Variant = NULL,
##Namespace Varchar(30) = NULL,
##ParameterName Varchar(40) = NULL,
##Standard Bit = 0,
##Timestamp DateTime = NULL
AS
SET NOCOUNT ON
DECLARE #ResultTable TABLE (AssemblyId Int, Namespace Varchar(30), ParameterName Varchar(40), Value Varchar(100))
-- Does loads of stuff and then inserts into #ResultTable table...
IF SomeContition IS TRUE RETURN
SET NOCOUNT OFF
-- Return the result
SELECT AssemblyId, Namespace, ParameterName, [Value]
FROM #ResultTable
RETURN

Resources