I have this data passed from my repo [1,2,3,4,5], it is a List<int> converted to a json string.
Now I want to insert it into the database. How should I write my query?
INSERT INTO CommitteeMember
SELECT
#committeeID,
* // how to call it?
FROM
OPENJSON(#membersJson) AS json
If I understand you correctly you want something like below:
CREATE TABLE CommitteeMember(committeeID INT, memberID INT);
DECLARE #membersJSON NVARCHAR(MAX) = '[1,2,3,4,5]';
DECLARE #commiteeID INT = 10;
INSERT INTO CommitteeMember(committeeId, memberId)
SELECT #commiteeID AS committeeId,value AS memberId
FROM OPENJSON(#membersJSON);
SELECT *
FROM CommitteeMember;
Dbfiddle Demo
Related
I'm a newbie in SQL and with programming languages in general. I'm trying to make a tabled function in SQL (SQL Server):
CREATE FUNCTION dbo.fn_Get_List (
#PAR_user_code INT
, #PAR_id_session INT
, #PAR_id_profile INT
, #PAR_days_check_from DATETIME
, #PAR_days_check_to DATETIME
, #PAR_register BIT
)
RETURNS #tb_return table(
num_prat int,
num_ipotec int,
typeipotec tinyint,
fee money,
stipulated_date smalldatetime,
expire_date smalldatetime,
renew_date datetime,
delete_date date,
authentication_date date,
prime money)
AS
BEGIN
and then I have to call it in another sql page. I've tried to use this syntax:
DECLARE #PAR_user_code INT
DECLARE #PAR_id_session INT
DECLARE #PAR_id_profile INT
DECLARE #PAR_days_check_from DATETIME
DECLARE #PAR_days_check_to DATETIME
DECLARE #PAR_register BIT
SELECT *
FROM fn_IPO_Get_Elenco_Ipoteche(#PAR_user_code,#PAR_id_session,#PAR_id_profile,#PAR_days_check_from,#PAR_days_check_to,#PAR_register)
If I run my SELECT I don't have any result, because my parameters are not initialized.
How can I insert values into them? What I want to do is take values from a table that I've created and where I have these attributes and when I run my webpage I'll fill them.
For example, if I log with ID Session = 1, I'd like to see it into #PAR_id_session and then valorise the other parameters with the user's choices on the webpage (I have multiple choice for #PAR_days_check_from). How can I do it if I don't know the values?
Intialization of variable in T-SQL:
DECLARE #PAR_user_code INT = 1;
or by using SET:
SET #PAR_user_code = 1;
The function arguments can be populated from table using CROSS/OUTER APPLY:
SELECT *
FROM my_table t
CROSS APPLY fn_IPO_Get_Elenco_Ipoteche(t.user_code, ...) f
WHERE t.col = ? -- any condition
I have a query like this when I pass the values into in operator in sql it shows:
Conversion failed when converting the varchar value '3,4,9' to data type int.
How can I solve the issue?
declare #values varchar(100)
set #values = '3,4,9'
select #values
select * from CmnItemType where ItemTypeID in (#values)
No. You can use string_split() or a similar user-defined function:
where itemtypeid in (select try_convert(int, value) from string_split(#values))
What I usually do is use table variable, like this one:
DECLARE #values TABLE (id INT)
INSERT INTO #values (id) VALUES (3),(4),(9)
SELECT id FROM #values
From that, you could simply do a join to your tables.
If you are creating a stored procedure, you can use a TVP to pass parameters, here is the Microsoft doc on that. With a TVP, your code can simply call your SP with a list and you will be able to join it in the SP.
Hope this will help.
I need assign the following SQL Server's query result value to the variable called #value1
SELECT *
FROM customer
WHERE apo_id = '2589';
How can I do this in SQL Server?
1 - First declare your variable of type table.
declare #value1 table(
--YOUR TABLE DEFINITION ex: ValueId int,
)
2 - Insert into your variable
insert into #value1 select * from customer WHERE apo_id = '2589';
Hope that helps, thanks.
It won't really be a variable but a table because you are selecting multiple fields (e.g. Select *) but, you can select INTO a temporary table like this:
SELECT *
INTO #myTempTable
FROM customer
WHERE apo_id = '2589';
Below #json contains 3 data object within an array. After using OPENJSON to extract these objects to a Table variable, please see the output attached.
DECLARE #json NVARCHAR(MAX);
SET #json = N'[{"Container":"MSKU2913236","Seal":"ML-TH4773979","Size":"20","Temperature":"-20","TareWeight":"3.132","CreatedDate":"02-02-2018 00:00:00","Comment":null,"NetWeight":"21.445","TempRec#":null},{"Container":"MSKU3432702","Seal":"ML-TH4773972","Size":"20","Temperature":"-40","TareWeight":"2.872","CreatedDate":"02-02-2018 00:00:00","Comment":null,"NetWeight":"23.932","TempRec#":"TR12345"},{"Container":"MSKU4043053","Seal":"ML-TH4773973","Size":"20","Temperature":"-20","TareWeight":"2.995","CreatedDate":"02-02-2018 00:00:00","Comment":null,"NetWeight":"22.4","TempRec#":null}]';
DECLARE #ContainerTable TABLE(
[Key] NVARCHAR(100),
[Data] NVARCHAR(MAX)
);
INSERT INTO #ContainerTable
SELECT [key], [value] FROM OPENJSON(#json)
SELECT * FROM #ContainerTable
Output
Objective is to replace the Key column values with the Container property value from json in the Data column for all 3 rows.
Expected Output
Note: Expected output is hard coded and it only shows the one row but same is required for all rows.
You could use JSON_VALUE:
INSERT INTO #ContainerTable([Key], [Data])
SELECT JSON_VALUE([value],'$.Container'), [value]
FROM OPENJSON(#json);
DBFiddle Demo
I want to store values from a SELECT statement into a variable which is capable of holding more than one value because my SELECT statement returns multiple values of type INT. This is how my SP looks like so far.
ALTER PROCEDURE "ESG"."SP_ADD"
AS
BEGIN
DECLARE #Id table(identifiers VARCHAR);
INSERT INTO #Id (identifiers) VALUES('axaa1aaa-aaaa-a5aa-aaaa-aa8aaaa9aaaa');
INSERT INTO #Id (identifiers) VALUES('bxbb1bbb-bbbb-b5bb-bbb4-bb8bbbb9bbbf');
DECLARE #tranID INT = (SELECT
DOCUMENT_SET_.DOCUMENT_SET_TRANSACTION_ID
FROM DOCUMENT_SET_TRANSACTION
WHERE DOCUMENT_SET_TRANSACTION.IDENTIFIER IN (SELECT identifiers FROM #Id));
END
Variable #tranID should be a list or an array to hold the ids. Is it possible to do it SQL Server?
You can declare a variable of type table
DECLARE #tblTrans TABLE (
tranID INT
);
INSERT INTO #tblTrans
SELECT DOCUMENT_SET_TRANSACTION.DOCUMENT_SET_TRANSACTION_ID
FROM ESG.DOCUMENT_SET_TRANSACTION
WHERE DOCUMENT_SET_TRANSACTION.IDENTIFIER
IN (SELECT identifiers FROM #envelopeId);
Depending on what you want to do with the values after this, you could declare a cursor to loop through them or select straight from the variable.
You could also look into using a temporary table depending on what scope you need.
Try this, only take the firs row of example. Do u try this?
select DOCUMENT_SET_TRANSACTION.DOCUMENT_SET_TRANSACTION_ID,
(STUFF((SELECT '-' + convert(varchar(max),DOCUMENT_SET_TRANSACTION.DOCUMENT_SET_TRANSACTION_ID)
FROM ESG.DOCUMENT_SET_TRANSACTION
FOR XML PATH ('')), 1, 2, '')) AS example
FROM ESG.DOCUMENT_SET_TRANSACTION