SQL Server - Contain Multiples Values - sql

I need retrieve a value of columm with SELECT. But, I have multiple values ...
I don't know what the user go select in checkbox...
Ex:
Insert Into MyTable (dados) Values ('a1') I want the result = Angulo 1
Insert Into MyTable (dados) Values ('a2';'a3') I want the result = Angulo 2
Insert into MyTable (dados) Values ('a3'; a1) I want the result = Angulo 3; Angulo 1
Insert into MyTable (dados) Values ('a6'; 'a7'; 'a4') I want the result = Angulo 6; Angulo 7;Angulo4
I am Trying with SELECT CASE WHEN. But it still fails...

I suspect you are asking how to use the IN keyword in your SELECT statements? It is a little unclear what you are trying to do.
Try this:
SELECT *
FROM MyTable
WHERE dados IN ('a6','a7','a4')
Assuming you have a table named MyTable and a column named dados with 3 rows in that table for a6, a7 and a4, this will return all the matches (in this case, all three rows).
Good luck.

When you say:
insert into MyTable(dados)
Values ('a6', 'a7', 'a4')
You are saying "I have one column to put data into called dados." Then, you are providing three values. This will fail in any database (even apart from the fact that the semicolons should be commas).
Perhaps you want:
insert into MyTable(dados)
Values ('a6;a7;a4')
That is only one value, a string.
This suggests a denormalized database. You might want three different rows in a table, one for each value, connected together by some key.

here are some examples if you're using sql server 2008 and above:
if(OBJECT_ID('tempdb..#dados') is not null)
DROP TABLE #dados
select top 100 * INTO #dados FROM
(
values(1,2,3),
(4,5,6),
(7,8,9)
) t(a,b,c)
select * FROM #dados
INSERT INTO #dados (a,b,c)
values(11,22,33),
(44,55,66),
(77,88,99)
SELECT * FROM #dados
INSERT INTO #dados (a,b,c)
SELECT * FROM
(
values(111,222,333),
(444,555,666),
(777,888,999)
) t(a,b,c)
SELECT * FROM #dados

If you want to insert multiple rows (not columns) the syntax is
Insert Into
MyTable (dados)
Values
('a1'),
('a2')

Looks like you're trying to ask for two things.
How to insert multiple values would be done in the following way:
Insert Into MyTable (dados) Values ('a6'),('a7'),('a4')
If you want to return the actual values 'Angulo' + the number, you can use the following:
CREATE TABLE MyTable
(
Dados varchar(255)
)
Insert Into MyTable (dados) Values ('a12')
Insert Into MyTable (dados) Values ('a2'),('a3')
Insert Into MyTable (dados) Values ('a3'),('a1')
Insert Into MyTable (dados) Values ('a6'),('a7'),('a4')
SELECT 'Angulo'+ SUBSTRING(dados,PATINDEX('%[0-9]%',dados),LEN(dados))
FROM MyTable
It will find the first number (assuming it's always the first number you're after) and get the rest of them. It will then append it with the prefix 'Angulo' (e.g Angulo1, Angulo7, etc)
If these aren't what you're after. Please can you explain further what you need.

Related

How to insert multiple rows into an SQL table when the values for one (or more) of the columns need to be derived from another table?

I cannot for the life of me find an answer to this extremely simple question.
What is the correct way to insert multiple rows into an SQL table when the values for one (or more) of the columns need to be derived from another table?
This is what I'm trying to do, using SQL Anywhere:
INSERT INTO Table (ColName1, ColName2, ColName3FK) VALUES
('foo', 'bar', (SELECT OtherTable.ID FROM OtherTable WHERE SomeCol = 'something')),
('doo', 'dar', (SELECT OtherTable.ID FROM OtherTable WHERE SomeCol = 'something')),
('goo', 'gar', (SELECT OtherTable.ID FROM OtherTable WHERE SomeCol = 'something'));
This throws an error saying 'Invalid value for INSERT' at the second SELECT statement. Sure enough, if I attempt to insert a single line the same way, it works fine:
INSERT INTO Table (ColName1, ColName2, ColName3FK) VALUES
('foo', 'bar', (SELECT OtherTable.ID FROM OtherTable WHERE SomeCol = 'something'));
Also, manually looking up the IDs I need and typing them in works with multiple lines:
INSERT INTO Table (ColName1, ColName2, ColName3FK) VALUES
('foo', 'bar', 42),
('doo', 'dar', 42),
('goo', 'gar', 42);
I have tried assigning the IDs I need to variables and using them in the INSERT statement with no luck (although I haven't spent too much time trying to do that, so I might have used incorrect syntax or sth). I have tried doing a similar thing using the WITH statement as well.
what you can do it's try to get the id outside of the query and after insert the rows like this .
var id = SELECT OtherTable.ID FROM OtherTable WHERE SomeCol = 'something' ;
(look at the id result and get the id )
INSERT INTO Table (ColName1, ColName2, ColName3FK) VALUES
('foo', 'bar', id),
('doo', 'dar',,id ),
('goo', 'gar',id);

How to get a key from another table in a insert into... Values SQL statement?

I am not very experienced in SQL statements and I am trying to combine a couple of statements to have less traffic to the db.(and to make sure no other actions can happen inbetween)...
I have two tables:
Table: R_LOTS
a.o. 2 columns: PK_R_LOT and LOTCODE
Table: R_LOTTRACKING
Columns: FK_R_LOT,TIMESTAMP,FK_MAGLOCATIES
I use the statement:
INSERT INTO R_LOTTRACKING (FK_R_LOT,TIMESTAMP,FK_MAGLOCATIES) VALUES (?,CURRENT_TIMESTAMP,?).
On the questionmarks I can fill in the values to send.
However, as you can imagine, I do not have the FK_R_LOT but I have the LOTCODE (of R_LOT).. Of course I can get the FK_R_LOT with a seperate SELECT PK_R_LOT FROM R_LOT WHERE LOTCODE=?; but is there a way to combine these statements?
I have seen some examples but then all information seems to come from the R_LOT table but I could not find a combination of VALUES and SELECT.
Summary:
I know: LOTCODE and FK_MAGLOCATIES
How to combine the statements to insert the row:
INSERT INTO R_LOTTRACKING (FK_R_LOT,TIMESTAMP,FK_MAGLOCATIES) VALUES (?,CURRENT_TIMESTAMP,?)
SELECT PK_R_LOT FROM R_LOT WHERE LOTCODE=?
Use a subquery:
INSERT INTO R_LOTTRACKING ( FK_R_LOT, ... )
VALUES ( (SELECT PK_R_LOT FROM R_LOT WHERE LOTCODE = ?), ... );
or use an insert select:
INSERT INTO R_LOTTRACKING ( FK_R_LOT, ... )
SELECT PK_R_LOT, ... FROM R_LOT WHERE LOTCODE = ?;

Insert values from a list in a single SQL statement

I have a list with values in it. I want to insert these values in a SQL table using a single INSERT statement.
Example: Say, there is a list with names (the size of the list is not constant). There is a STUDENTS table with NAME column. I want to insert in the STUDENTS table the names from the list using a single INSERT statement.
Right now I loop through the list and insert the value in the table. That means, the number of insert statements is equal to the length of the list.
List<String> Name;
foreach (String s in Name)
{
INSERT INTO STUDENTS (NAME) VALUES (s)
}
Is there a way I can accomplish this in a single SQL INSERT statement? Any help is much appreciated.
In MySql you can do it like this
INSERT INTO tbl (f1,f2) VALUES(1,2),(3,4),(5,6)...(55,77);
For mssql dialect all the same
Union can be used to achieve this, though technically each union is it's own statement.
short demo:
create table #i(hello int)
insert into #i select 1 union all select 2
select * from #i
drop table #i
There are other / better ways of populating tables from lists...those depend on what database you are on.
To do this with a single INSERT is with a SELECT.. you'd have to convert your list into this:
INSERT STUDENTS(NAME)
SELECT 'Tom'
UNION ALL
SELECT 'Bill'
UNION ALL
SELECT 'Sarah'
If your list was in a temporary table, you could use the following syntax:
insert into tblname
(columnlist)
select name from temptable;
with string.join
private void insertInto(string tablename, List<Values> list)
{
context.Database.ExecuteSqlRaw($#"INSERT INTO {tablename}(number,comment) VALUES
{string.Join(',',list.ToArray().Select(r =>$"
({r.value},'{r.comment}')").ToList())}");
returns list data and result : VALUES(1,'hello'),(5,'hello2').....

insert statement one column from another table rest of the columns is values

would like to know how to write SQL query, when you want to use the insert statment and fill it with 2 values and then a value from a external table?
Something like this.
INSERT INTO [PPLAT_KPI].[dbo].[KPI_Values]
(TS,
Value,
ID)
VALUES
('2010-02-04'
,'200',
SELECT KPI_Parameter.ID
FROM [PPLAT_KPI].[dbo].[KPI_Parameter]
where KPI_Parameter.Site = 'XXX' and KPI_Parameter.Plant = 'vv';)
Please try:
INSERT INTO [PPLAT_KPI].[dbo].[KPI_Values]
(TS,
Value,
ID)
SELECT
'2010-02-04',
'200',
KPI_Parameter.ID
FROM [PPLAT_KPI].[dbo].[KPI_Parameter]
where KPI_Parameter.Site = 'XXX' and KPI_Parameter.Plant = 'vv';
Use INSERT INTO...SELECT like this: http://www.w3schools.com/sql/sql_insert_into_select.asp

Comma separated list of values as different rows, not as fields for one row, for an SQL INSERT

I want to make many inserts in a single SQL statement, pretty much like
INSERT INTO xxx SELECT field FROM zzz
But with values I don't have in a table, and are just a list of literals.
Normal way would be executing one SQL query for each value, but if there exists a standard SQL statement, or MySQL-specific, it would be great.
insert into xxx (fields) values (values1), (values2), (values3)
eg insert into mytable (name, desc) values ('name1','desc1'), ('name2','desc2'), ('name3','desc3'), ('name4','desc4')
insert into xxx(afield)
select 'a'
union
select 'b'
union
select 'x'
will give you a table like that :
afield
a
b
x