I have a stored procedure that I want to use to create one row in a parent table [Test]
and multiple rows in a child table [TestQuestion]. The parent and child table both have primary keys that are identity datatype. Here is what the child table looks like with some not relevant columns removed:
CREATE TABLE [dbo].[TestQuestion] (
[TestQuestionId] INT IDENTITY (1, 1) NOT NULL,
[TestId] INT NOT NULL,
[QuestionNumber] INT NOT NULL
);
Inserting into the parent table is easy as all parameters are supplied to the SP and I just map these to an insert and perform the insert. But the child data table ids are given as a parameter #qidsJSON containing ids in a JSON form like this:
parameterList.Add(new SqlParameter ("#qidsJSON", qids.ToJSONString()));
["3CEFF956-BF61-419E-8FB2-9D6A1B75B909","63E75A2D-9F45-43CC-B706-D9890A22577D"]
Is there a way that I can use TransactSQL to take the data from my #qidsJSON and
have it insert a row into the TestQuestion table for every GUID that appears in the parameter?
Alternatively is there another way I could pass data in a parameter that contains mulitple GUIDs? I am using C# to formulate the input data from a C# List so I could create the data for the input parameter in any form that would be most easy for the stored procedure to use.
You can use Table variable parameter for your stored procedure :
CREATE TYPE GuidList AS TABLE (Id UNIQUEIDENTIFIER)
CREATE PROCEDURE test
#Ids dbo.GuidList READONLY
AS
Use following reference in order to use table variable parameter in C#:
How to pass table value parameters to stored procedure from .net code
C# and Table Value Parameters
Related
I have programmed a function with
1) Create Table in Tempdb
Create Table in Tempdb
Create Table Value
while , If commands
Delete values from Table
Inserting values into above tables
Checking existence of table
IF OBJECT_ID('tempdb..#Results') IS NOT NULL
DROP TABLE #Results
inside of it.
The results are in Temp tables show be backed
can I use a Table Value function for it? or it just should an insert
can I use a Table Value function for it? or it just should an insert
No, DML functions are not allowed inside a function. You need to use a stored procedure to perform the operations you have mentioned.
Read following link for more details on what is allowed and not allowed in a function.
SQL SERVER – User Defined Functions (UDF) Limitations
I'm working with SQL Server, and have a table and type like below ...
CREATE TABLE Foo (
Id BIGINT,
Age INT,
DateCreated DATETIME2
);
CREATE TYPE FooRow AS TABLE (
Id BIGINT,
Age INT,
DateCreated DATETIME2
);
I use the FooRow type in a stored procedure, as the return type, in which I query some records from Foo table, store them in a FooRow variable, do some other things with them, and finally output the FooRow variable.
Is there a way to reduce this repeated typing of the Foo table schema to create the FooRow type?
Is it possible to express a stored procedure variable's type as an existing table, or something?
DECLARE #foorows TABLE Foo
If not, is it possible to create a type, like FooRow, and just say "use the columns from Foo"?
You can declare table variables to have a certain table type, but this type has to be created with a CREATE TYPE statement (it cannot be another existing table). However, you can script the definition of an existing table (using SSMS) and change only the first line manually.
You can also define a table-valued function to return a certain table type. The advantage of a table-valued function (over a stored procedure) is that they may be called from a query (or even from a view). Although there are additional restrictions of what a table-valued function can do (mainly, it cannot modify any data that resides outside the function), you should consider using them instead of nested stored procedures (in other words, when the result of the stored procedure must be processed further in another procedure).
Have a table where a column houses a stored procedure to be executed for each row in a cursor or loop this procedure has table valued parameter as input parameters the procedure being called for each row gets its values from the other columns in the same table based on an update statement using dynamic sql which populates the column and gives the procedure its values . One of the columns being used to supply values to the procedure has a string value consisting of many rows however the for XML PATH ('') was used to convert the value to a single one line string. This string needs to passed into the a user defined table type input parameter for the procedure .
How can I get this string value to be inserted especially when it has multiple rows sometimes , meaning I need to make multiple inserts into the user defined table type variable for that row of execution. *
You can try this.
Insert into Table tablename (col1,col2,..,coln) values (call storedprocedurename(arg1,arg2,..,argn))
Note : No.of attributes in insert query should equal with no.of attribute which stored procedure returns. And also in same order
I want to develop a BizTalk orchestration. Which should insert multiple records into multiple DB tables and retrieve inserted records from multiple DB tables, in single instance of orchestration. For this requirement, I'm able to insert the data in one instance, but seeing difficulty to retrieve the inserted data for that instance, as all the records has unique values for each record. For my situation, I should use stored procedures, to apply some other business logic. So I have 2 different methods by using "Wcf_Custom Adapter composite feature" by calling stored procedures, as stated below.
-> Method1
I have to develop a Stored procedure, which takes LoadDate("2016-05-12 10:11:22.147") as parameter along with inserting values and it will take care of inserting the records for that instance, by keeping the given LoadDate. Then immediately it will call Get stored procedure, which takes the LoadDate("2016-05-12 10:11:22.147") as parameter, then it will retrieve the recently inserted records from DB based on LoadDate value.
I know, Retrieving the data based on a date value from sql server is a bad practice and it will give performance issues too.
-> Method2
I'll design the inserting tables, with bool data type column name "New" and value will be 0 or 1. I'll develop a Insert Stored procedure, which inserts the data by giving the "New" column value as "1". Then immediately it will call Get stored procedure, which will not take no parameters, then it will retrieve the recently inserted records which are having "New" column indicator "1" from DB tables. Once it retrieves the data, then it will update "New" column value to "0".
I prefer this method2. But, do we have better option?
As #johns-305 mentioned in his comment. You shall use table value param in your sp. and assembly all your data in orchestration then make a call to this sp.
A sample sp may like below:
CREATE TYPE [dbo].[SampleDataTable_Type] AS TABLE(
[ID] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
CREATE PROCEDURE [dbo].[sp_InsertSampleTableData]
(
#LoadDate DATETIME,
#data [SampleDataTable_Type] READONLY
)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO your_table(id, name,)
SELECT id, name FROM #data;
--Do whatever you want
SET NOCOUNT OFF
END
GO
I think your stored procedure may look like this:
create procedure myProc
#a int, #b varchar(100)
as
insert myTable(a,b,c)
OUTPUT inserted.* --this line
select a,b,c
from somewhere
where a=#a and b=#b
I have a stored procedure to insert a row, with a list of values which came as input parameters, into a table (tmp).
This table mirrors another table (data), and I'm looking for a way to use the same SP to insert a row from data into tmp.
I've seen Pass result of a query into stored procedure, but I can't put INSERT in a UDF.
Is there an easy way, not requiring
a query which puts all the values into variables and sends the variables?
a table-typing or other additions to our already-too-full database
Edit The desired result would be a way to write something like
EXEC sp_insert (SELECT c1,c2,c3 FROM data)
Maybe you can use variable table as the parameter for the SP (search for this)
It needs to define user defined table type , but I'm using this method.
Table used as a parameter can get values from inserting or selecting from other table.
alter procedure sp_insert
#table your_table_type readonly
AS
BEGIN
--here you take a row (maybe the only one) from #table and insert it to TMP
END