How to pass two list of values to stored procedure in SQL Server? - sql

So i am trying for writing a procedure which need to accept the input values in form of two lists as follows:
execute ProcedureForInsert 'john,marry,tom' '1,2,3' 40
where first parameter is list of Varchar type and second is list of int type and length of both lists are to be same and third parameter is also of int type.
My script is to use these two list values to insert into some table along with third parameter.
How to store and iterate two lists for creation of Insert Query .
Thanks in advance.

You need a "split string" function that takes a comma separated list and produces a table of the individual strings as output. There are a lot of these to be found, but look here to start:
How to split a comma-separated value to columns

Related

How to check if any values of an array is a substring of a column value in SQL Server 2017?

I have a table with a column equipments VARCHAR(100). I put comma-separated equipment names as string into this column.
A sample value might be:
"PKM_119160.000, PKM_119160.135"
Now, I want to filter the values of this table using an array of equipment names, checking if any value of that array is a substring of equipments. So If I pass an array like
["PKM_119160.000", "PKM_119160.216"]
I need to fetch the above row as "PKM_119160.000" is a substring of "PKM_119160.000, PKM_119160.135". I could use IN if the equipments column contains single equipment name.
How can I get the proper values here? What should I do now? TIA

Store SQL query result (1 column) as Array

After running my query I get 1 column result as
5
6
98
101
Is there a way to store this result as array so that I can use it later
in queries like
WHERE NOT IN ('5','6','98','101')
I am aware of storing single variable results but is this possible?
I can not use #Table variable as I will be rerunning the query again in the future and it goes out of scope
There are multiple way of storing those column data like using Temporary Tables or View or Table valued function but IMO there is no need of storing that column data anywhere. You can directly use that column in any query saying below (or) perform a JOIN which would be much better option than NOT IN
select * from
table2
where some_column not in (select column1 from this_table);
While this method is not recommended, storing an array in a single column can be done using CSV's(Comma Separated Values). Simply create a VARCHAR array and store it by storing a string containing the values in a specific order. Basically store all of your values into a string with each value being separated by a comma in that string. Store that into a column of your choice. You can later fetch the string and parse it with a string parser i.e using the .split() function in python. AGAIN I do not recommend doing this, I would instead use multiple columns, one referring to each value and access them that way instead
Using separate columns would make it easy to use in a Stored Procedure.

Nested table type in SQL server

How can I define a table type in SQL server when one of the columns is an array of decimals?
I'm trying to pass to stored procedure an .NET object one of the fields of which is an array of decimals.
Thanks
t-sql does not support arrays.
You do, however, have some options: here are 3 of them, from the best to the worst:
Create 2 table types, have a column in one act as a foreign key to the other.
Create a table type with a varchar(max) column that will hold your decimal
values as a comma delimited string.
Create a table type with an xml data type column.

Stored procedure that takes list of string as input and insert missing rows then return them

I have a table Names
Id Name
+----+---------------+
1 John
2 Kate
3 Mark
I want to create a stored procedure that does the following:
1) take a list of names as string as an input parameter
I have done some researches about this but couldn't find the best way to do it. I will call the stored procedure from the entity framework in a C# application. I was thinking of passing the names in one string separated with a comma and the split them in the procedure. But can't figure out how this is done.
2) for each name in the list, if the name does not exist in the Name column, insert a new row for it.
How can I do a switch case if it exists and insert it if not
3) Select * rows that are in the input list.
After adding all the missing Names, I want to select all the names that were in the input list with their id
Can this be done in one stored procedure, or do I have to divide them into multiple.
I am looking for hints on how to do each step, and if they can be combined.
Thanks
Keep your DB side lean and leave logic on the app side, especially if you have grumpy DBA's.
Use a MERGE/Upsert instead.
Check out this SO post.
If you pass a comma delimited list into a stored procedure as a parameter, you are going to need to understand how to use charindex, left, substring and right
As you split out each name - you would add them into a temporary table or a table valued variable.
Your decision about whether to insert the new names into the Names table would be made using an exists() subquery on an insert statement.
You could then, finally, fashion a select statement to join your temp table/table valued variable back to your Names table to pull out all of the keys (including the new ones) and pass them back to your front end.

Array parameter for TADOQuery in Delphi 2010

I need to execute a simple query:
SELECT * FROM MyTable WHERE Id IN (:ids)
Obviously, it returns the set of records which have their primary key 'Id' in the given list. How can I pass an array of integer IDs into ADOQuery.Parameters for parameter 'ids'? I have tried VarArray - it does not work. Parameter 'ids' has FieldType = ftInteger by default, if it matters.
There is no parameter type that can be used to pass a list of values to in. Unfortunately, this is one of the shortcomings of parameterized SQL.
You'll have to build the query from code to either generate the list of values, or generate a list of parameters which can then be filled from code. That's because you can pass each value as a different parameter, like this:
SELECT * FROM MyTable WHERE Id IN (:id1, :id2, :id3)
But since the list will probably have a variable size, you'll have to alter the SQL to add parameters. In that case it is just as easy to generate the list of values, although parametereized queries may be cached better, depending on which DB you use.
The IN param just takes a comma separated string of values like (1,2,3,4,5) so I assume you set the datatype to ftstring and just build the string and pass that...? Not tried it but it's what I would try...