I'm having a procedure and some custom types to pass some data to it:
CREATE TYPE StringList AS TABLE
(Value NVARCHAR(255));
GO
CREATE TYPE KeyValueList AS TABLE
(
Id uniqueidentifier,
Value NVARCHAR(255));
GO
CREATE PROCEDURE
#update_AttributeFormel(
#modelPrefix NVARCHAR(255),
#definitionNeutralName NVARCHAR(255),
#newFormula NVARCHAR(255),
#providersToRemove StringList READONLY,
#providersToAdd KeyValueList READONLY)
AS ....
The ManagementStudio does not show any error (as it would when i comment out the type declaration) but when i execute the script, i'm getting the following errors:
Msg 2715, Level 16, State 3, Procedure #update_AttributeFormel, Line 2
Column, parameter, or variable #4: Cannot find data type StringList.
Parameter or variable '#providersToRemove' has an invalid data type.
Msg 2715, Level 16, State 3, Procedure #update_AttributeFormel, Line 2
Column, parameter, or variable #5: Cannot find data type KeyValueList.
Parameter or variable '#providersToAdd' has an invalid data type.
Implementation as explained here:
http://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/
http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters
Does anyone have an idea why this happens?
I am pretty sure it is due to creating the procedure in the tempdb space, using CREATE PROCEDURE #update_AttributeFormel. See this SQL Fiddle where it is working perfectly when the types and the procedure are in the same database.
The code is replicated below:
CREATE TYPE StringList AS TABLE
(Value NVARCHAR(255));
GO
CREATE TYPE KeyValueList AS TABLE
(
Id uniqueidentifier,
Value NVARCHAR(255));
GO
CREATE PROCEDURE update_AttributeFormel(
#modelPrefix NVARCHAR(255),
#definitionNeutralName NVARCHAR(255),
#newFormula NVARCHAR(255),
#providersToRemove StringList READONLY,
#providersToAdd KeyValueList READONLY)
AS
SELECT * FROM #providersToRemove
GO
declare #SL StringList;
insert #SL select 'abcdef' union all select 'ok';
declare #KVL KeyValueList;
exec update_AttributeFormel null,null,null,#SL,#KVL;
You have to create the custom data type in tempdb for it to be recognized in a different database.
Related
Can anybody help me,as I want to declare a cursor with a variable which is of table type and I am getting invalid data type for parameter. In a stored procedure in SAP HANA
Create TYPE "TT_GUID" AS TABLE ( "guid" VARCHAR(32));
DECLARE CURSOR C_cursor( var_guid "TT_GUID") FOR
Select zval_001,zval_002
FROM "table1"
Where "guid" = :var_guid ;
I am trying to use table-valued parameter to pass a column to "IN" clause in SQL Server. To do so I need to declare a type of table.
The SQL expression:
DECLARE TYPE myType AS TABLE( myid varchar(50) NULL);
is giving the error:
'myType' is not a recognized CURSOR option.
Replacing DECLARE to CREATE is working good.
Using "CREATE" is requiring to drop this type in any next SQL calls like:
IF TYPE_ID('myType') IS NOT NULL DROP TYPE myType;
But I would like to use the type "myType" just within this SQL expression only.
How to declare a type as table without creating it permanently and without deleting it in all requests?
DECLARE requires the following sinthax:
DECLARE #LastName NVARCHAR(30), #FirstName NVARCHAR(20), #Country NCHAR(2);
With DECLARE, you can:
To assign a variable name(using #).
To assign a Variable Type
To assign a NULL default Variable value
So "DECLARE TYPE" is not a valid statement.
Have a look to temporary tables in sql.
You can create a temporary table in sql with the following code:
CREATE TABLE #TempTable
(
name VARCHAR(50),
age int,
gender VARCHAR (50)
)
INSERT INTO #TempTable
SELECT name, age, gender
FROM student
WHERE gender = 'Male'
With a temporary table, you can store a subset of data from a standard table for a certain period of time.
Temporary tables are stored inside “tempdb” which is a system database.
I created a stored procedure in sql server to feed SSRS to allow it to accept Multiple values.
I have created it and when I used it in my report or execute it in sql server I have the following error message. Is there anything i am missing? Thanks
Msg 207, Level 16, State 1, Line 35
Invalid column name 'London'.
This is my sample data. feel free to create table with it
DECLARE #MyTables AS TABLE (ID INT, City VARCHAR(100))
INSERT INTO #MyTables VALUES
(1,'London'),
(2,'Chester'),
(3,'Luton'),
(4,'New York'),
(1,'London'),
(2,'Chester'),
(5,'Paris'),
(5,'Paris'),
(2,'Chester'),
(2,'Chester')
SELECT * FROM #MyTables
This is my code for the dynamic stores procedure
CREATE PROCEDURE dbo.CitiesGroup
#Cities NVARCHAR(Max) -- this are the parameters
AS
BEGIN
DECLARE #sqLQuery VARCHAR(MAX)
Declare #AnswersTempTable Table
( ID INT,
City VARCHAR(250)
)
SET #sqlQuery =
'SELECT
ID,
City
FROM MyTables
where Convert(nvarchar(Max),City) IN ('+#Cities+')
Insert into #AnswersTempTable
exec (#sqlQuery)
select * from #AnswersTempTable'
END
Thanks
EXEC dbo.CitiesGroup 'London'
Error meg
Msg 207, Level 16, State 1, Line 32
Invalid column name 'London'.
There is another way to do this. Instead of passing the values into a dynamic query, why not split the parameter using a function? This article written by Aaron Bertrand demonstrates different ways on how to split string in sql server.
Once you have selected one of the functions, you can simply rewrite your stored procedure without creating a dynamic query inside.
CREATE PROCEDURE dbo.CitiesGroup
#Cities NVARCHAR(Max) -- this are the parameters
AS
BEGIN
-- simplified query
-- write your complex logic here
SELECT ID, City
FROM MyTables
WHERE City IN (SELECT Item FROM dbo.SplitStrings_CTE(#Cities, N',');)
END
Usage:
EXEC dbo.CitiesGroup 'London'
GO
EXEC dbo.CitiesGroup 'London,New York,Paris'
GO
Useful Link:
Split strings the right way – or the next best way
Alternatively, if you don't need to use a stored proc, you can simply put your query directly in the dataset as
SELECT ID, City
FROM MyTables
WHERE City IN (#Cities)
There is no need for dynamic sql as SSRS will do this for you. Just makes sure the SSRS parameter name and the Variable in your SELECT statement are identical (case-sensitive)
I have a list ClaimData in C# and it has three items Date, Type and Description
There can be multiple rows in this as below,
ClaimData
Date Type Description
01/02/2012 "Medical" "Its a medical"
05/02/2013 "Theft" "Its a Theft"
01/02/2014 "Test" "Its a Test"
I want to pass this whole data to a stored procedure in one go to the sql server, so that I can reduce the database hits. I have written stored procedure which can iterate through this list and insert them in a table.
How to achieve by manipulating the list object could be passed to the stored procedure as a parameter?
You will need to do a couple of things to get this going, since your parameter is getting multiple values you need to create a Table Type and make your store procedure accept a parameter of that type.
Since you are passing a TABLE as a parameter you will need to create a TABLE TYPE something as follows
TABLE TYPE
CREATE TYPE dbo.ClaimData AS TABLE
(
[Date] DATE
[Type] VARCHAR(50)
[Description] VARCHAR(100)
)
GO
Stored Procedure to Accept That Type Param
CREATE PROCEDURE mainValues
#TableParam ClaimData READONLY --<-- Accepts a parameter of that type
AS -- Note it is ReadOnly
BEGIN
SET NOCOUNT ON;
--Temp table to store the passed values
-- since the passed parameter is only Read only and you
-- cannot make any changes to the parameter so if you need to
-- manipulate the data inside parameter you will need to get it
-- into a Table vaiable.
-- Declare a Table variable
DECLARE #tmp_values table(
[Date] DATE
[Type] VARCHAR(50)
[Description] VARCHAR(100)
);
--Get values into that Table variable
INSERT INTO #tmp_values ([Date],[Type],[Description])
SELECT [Date],[Type],[Description] FROM #TableParam
-- Do other cool stuff with your passed data
SELECT * FROM #tmp_values --<-- For testing purpose
END
EXECUTE PROC
Declare a variable of that type and populate it with your values.
DECLARE #Table ClaimData( --<-- Declare a variable of your type
[Date] DATE
[Type] VARCHAR(50)
[Description] VARCHAR(100)
);
-- Populate the variable
INSERT INTO #Table ([Date],[Type],[Description])
SELECT [Date],[Type],[Description] FROM Source_Table
EXECUTE mainValues #Table --<-- Stored Procedure Executed
I sent all three column as a string using string builder and delimeter '|'
DateString = '01/02/2012|05/02/2013|01/02/2014'
TypeString = 'Medical|Theft|Test'
DescString = "Its a medical|..."
On database side I used a function to delimit these strings and inserted all these values in a temp table. This solved my problem.
I want to capture Radio button values in db so i created table with datatype bit
But the problem when am create stored Procedure as
CREATE PROCEDURE Mt_Vacancy_mainPreference_insert
(
#post_name varchar(20),#preference varchar(20),gender bit,No_of_vac int
)
AS
Begin
insert into Mt_Vacancy_mainPreference values(#post_name, #preference, #gender, #No_of_vac)
end
RETURN
It says Incorrect Sytax near gender
Must produce scal value
What is the problem
You are missing the # prefix for your last 2 parameter names.
You forgot to decorate the input variables;
gender bit,No_of_vac int
Should be
#gender bit, #No_of_vac int