LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

INSERT & SELECT data in same recordset

I want to insert an user in a table, and get the ID back in one recordset.

In Microsoft SSMS it works fine, but INSERT and SELECT in same recordset will not work in Labview😡

No errors and no Result data?

 

Why can't I use INSERT and SELECT in same recordset?

 

//Jesper

LV_SQL_CreateUser.jpg

0 Kudos
Message 1 of 17
(1,279 Views)

Because it is not supported...

 

Speculation:

The query might return a multi table, and I have noticed those are not supported.

 

The code doesn't care the first table is empty (or null), it simply doesn't look further.

0 Kudos
Message 2 of 17
(1,271 Views)

So if I want to insert data into a table, and get the ID back, it's not possible in LabView?

0 Kudos
Message 3 of 17
(1,267 Views)

@Discrusio wrote:

So if I want to insert data into a table, and get the ID back, it's not possible in LabView?


Of course it is: do 2 queries.

0 Kudos
Message 4 of 17
(1,262 Views)

I'm new to SQL so maybe I'm doing it the wrong way.

 

I have created a SQL-procedure with username input.

The procedure checks if the username not present in the table, then it will insert the new user.

Then it will return the index for the user.

 

The procedure will not work in Labview, because both INSERT and SELECT is present in the procedure.

0 Kudos
Message 5 of 17
(1,249 Views)

@Discrusio wrote:

I'm new to SQL so maybe I'm doing it the wrong way.

 

I have created a SQL-procedure with username input.

The procedure checks if the username not present in the table, then it will insert the new user.

Then it will return the index for the user.

 

The procedure will not work in Labview, because both INSERT and SELECT is present in the procedure.


The insert and select query is really two queries. So, split it up into two queries. First do the insert, then the select. Fetch the select results.

0 Kudos
Message 6 of 17
(1,209 Views)

Another option is to create a stored procedure that does the same, but yeah 2 queries might be the better solution.

0 Kudos
Message 7 of 17
(1,187 Views)

Both suggestions are correct. Running the two queries you have in the very first post is not supported (as you have it written) but you could use a stored procedure as well, but reading that you were a beginner in SQL, I'd pass on that until you are more familiar with the T-SQL language.

 

This is how I would attack it.

 

  • Check for Bob first (SELECT statement)
  • If he isn't there, Insert a record for Bob (INSERT statement)
  • Get his information (SELECT statement)
Message 8 of 17
(1,185 Views)

I started with a procedure, with INSERT data and SELECT inserted data_id, and it didn't work with Labview.

Then a tried with the simple attached script instead, with same result.

 

But, now I found a solution.

Instead of the SELECT cmd, I can use OUTPUT INSERTED cmd.

 

INSERT INTO [dbo].[USERS] ([User_Name])
OUTPUT INSERTED.[USER.ID]
VALUES (@User)

 

I updated my Procedure with the OUTPUT cmd, so now it will create a new user, if the user not exist, and rerurns the id for the user.

Message 9 of 17
(1,153 Views)

@Discrusio wrote:

I started with a procedure, with INSERT data and SELECT inserted data_id, and it didn't work with Labview.


It always worked for me.

 

I think your code didn't work.

 


@Discrusio wrote:

Then a tried with the simple attached script instead, with same result.


No attachment

 


@Discrusio wrote:

But, now I found a solution.

Instead of the SELECT cmd, I can use OUTPUT INSERTED cmd.

 

INSERT INTO [dbo].[USERS] ([User_Name])
OUTPUT INSERTED.[USER.ID]
VALUES (@User)

 

I updated my Procedure with the OUTPUT cmd, so now it will create a new user, if the user not exist, and rerurns the id for the user.


That's nice. Didn't know SQL could do that. Thanks (and a kudo, your 1st!) for teaching me something!

Message 10 of 17
(1,133 Views)