I am trying to make this query work but i keep getting error.
insert into Table1 (CL1,CL2) Values ('TEST',CL2)
SELECT CL2 from Table2 where ID = 2
I am trying to take data from table 2 and put it in table 2 with the name TEST
Table1
is Empty
Table2
ID=2,SUP,SUP,SUP
if any one can help it would be great
insert into Table1 (CL1,CL2) SELECT 'TEST', CL2 from Table2 where ID = 2
Is this what you want:
INSERT INTO Table1 (CL1,CL2) VALUES ('TEST',(SELECT CL2 FROM Table2 WHERE ID=2))
Related
I'm working with Dapper and .Net 6.0... I have to do an insert from table1 to table2... in the insert the columns match each other... the only column is "toID" of table1... which does NOT match the column of table2 (that's why I put a 4 in it) but I have to make it auto-increment so that for each insert there is an incrementing sequence
var sql =
$"INSERT INTO table1 (toId,teId,dateShift,SectorOrigen) SELECT 4,teID,#dateModify,#LastSector FROM table2";
That is to say... that when I generate an insert the ID = 1, then another ID = 2 and so on continuously
Any advice??
You can create and use the sequence https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16
I want to insert some data in Table1 in my database selecting it from another Table2 but the Table1 has another attribute for username (ComputerName).
What I have tried:
INSERT INTO [AffecAnalytique].[dbo].[C9_V] ([C9], [V], [OID], [USERMODIF])
SELECT [C9], [V], [OID] FROM [AffecAnalytique].[dbo].[C9_V]
WHERE [OID] = 'CEC4F038E3954AC79DBF7EC38B02171F' , 'AHE'
I think you are looking for something like this :
INSERT INTO [AffecAnalytique].[dbo].[C9_V] ([C9], [V], [OID], [USERMODIF])
SELECT [C9], [V], [OID], 'AHE' FROM [AffecAnalytique].[dbo].[C9_V]
WHERE [OID] = 'CEC4F038E3954AC79DBF7EC38B02171F'
I have two tables where I have a situation like this:
Table1
column1
column2
column3
column4
column5
Table 2 structure:
Table2
Col1
Col2
Col3
Table1FK
My table 1 currently has 3 records inside it, and the table 2 shall contain more records than table 1 (one to many relationship between these two). I want to assign an even number of records from Table 2 to table 1 FK's.
For example:
If table 2 has 20 records and if table 1 has 3 records
I will divide these two and get an even number, 6.66 in this case.
So the table1 PK's should be assigned like
6-6-8
or
7 7 6 (this one is more even)
And then table 1 PK under identity let's say 1500 would have 7 of it's corresponding FK's in table 2 , 7 for Identity 1501 , and 6 for identity 1502
Starting point is that I should divide these:
var evenAmountOfFKs = table2.Count()/table1.Count();
What would be the next step here, and how could I achieve this ?
Can someone help me out?
No matter how weird the requirement seems, the math was fun.
At first, we have to determine the number of parents:
DECLARE #NoParents int = (SELECT COUNT(*) FROM Table1);
Both parents and children can be numbered starting with 0, and then a child can be assigned to parent x with x = ChildNo % #NoParents:
DECLARE #NoParents int = (SELECT COUNT(*) FROM Table1);
WITH
Parents AS (
SELECT column1, column2, column3, column4, column5,
ROW_NUMBER() OVER(ORDER BY column1)-1 AS ParentNo
FROM Table1
),
Children AS (
SELECT Col1, Col1, Col1,
(ROW_NUMBER() OVER(ORDER BY Col1)-1) % #NoParents AS ParentNo
FROM Table2
)
SELECT p.column1, p.column2, p.column3, p.column4, p.column5, c.Col1, c.Col1
FROM Parents p
INNER JOIN Children c ON p.ParentNo = c.ParentNo;
This will produce an "even" assignment.
I'm working on a C# project that incorporates SQL, mainly using entities and linq.
Let's say I have two tables.
Table 1
primary key | name | date
Table 2
primary key | fktable1 | price
I would like to display Name, Date AND price in a datagrid.
How can I pull the two - or one if we do it vice versa - values out of table 1 by only referencing the Foreign Key(Which is the Primary Key of Table 1) ?
If your only problem is the sql statement you could use something like this (or "join"):
select t1.name, t1.date, t2.price from table1 t1, table2 t2 where t1.primary_key = t2.foreign_key;
If you have other problems please provide us some more details...
Supposing you have your data in IEnumerable<Table1> Table1List and IEnumerable<Table2> Table2List, you can use LINQ :
Table1List.Join(Table2List,
t1 => t1.PrimaryKey, // replace PrimaryKey by your actual field storing primary key
t2 => t2.fkTable1,
(t1, t2) => new { t1.Name, t1.Date, t2.Price }
)
This will correspond more or less to the SQL query :
SELECT t1.Name, t1.Date, t2.Price
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.PrimaryKey = t2.fkTable1
Yes you can select any column by joining the tow tables.
example: SELECT Name, Date AND price FROM table 1 INNER JOIN table 2 ON table1.key=table2.fktable1
i have two table. table1 and table2.each table is having three columns.
each table columnsname is different.i apply the query for table .i want one columns value for table1
and i want two columns value for table2.i want to write one query for each table and apply different condition for each table.but ,i want to result in only one query
You can concatenate to results with union
select col1 as A, col2 as B, col3 as C from table1 where col1 = 'foo'
union all
select colA as A, colB as B, colC as C from table2 where colB = 'bar'
use joins , it can be retrived
select table1.colname,table2.colname from table1, table2
where table2.colname=table2.colname.
SELECT tbl1.columnName,tbl2.columnName1,tbl2.columnName2
FROM table1 tbl1
OUTER APPLY
(
SELECT columnName1,columnName2 FROM table2 WHERE table2.field_fk = tbl1.filedId
)tbl2