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'
Related
This is just a lazy thought. I have a table with about 9 columns names. Is there any way I can use SQL statement to return only the Column names? The normal way is to write out my column names from
SELECT * FROM tableName;
statement but was wondering if I can get the column names with SQL statement.
Any Ideas would be appreciated.
Thanks You!
SELECT COLUMN_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tableName' AND TABLE_SCHEMA='dbo'
Should do it
SET FMTONLY ON
SELECT * FROM tablename
Open you Microsoft SQL Server Management Studio, in New Query, Just type your Table name and select it and then press ALT + F1 Key, it will give all details about table
You can query the syscolumns table for table column metadata.
SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = (SELECT id FROM sysobjects WHERE [Name] = 'TableName')
See this image that query return values
Referred from : How can I get column names from a table in Oracle?
and another one way
select column_name,* from information_schema.columns
where table_name = 'YourTableName'
order by ordinal_position
Using SQL Server xml raw you get the column names from any sql query. Replace 'select top 1 * from [TABLENAME]' from the example below. Query must return at least 1 row of data and remember to use top 1.
declare #t1 table (x xml)
declare #t2 table (x xml)
declare #t3 table (x xml)
insert into #t1 select cast( (select top 1 * from [TABLENAME] for xml raw) as xml)
insert into #t2 select y.r.query('.') from #t1 cross apply x.nodes('/row') as y(r)
insert into #t3 select t2.n.value('local-name(.)', 'varchar(max)') from #t2 t cross apply x.nodes('//#*') as t2(n)
select replace (convert (nvarchar, x),'_x0020_', ' ') from #t3
Try following SQL statement to get column name.
SELECT column_name
FROM information_schema.columns
WHERE TABLE_NAME='tableName'
I'm trying to write a shopping basket into a order + orderline in a sql database from C# asp.net.
the orderline will contain a ordernumber, total price, productid, quantity etc. for every item in the basket. The order itself will contain the ordernumber as primary key and will be linked to the different lines through it.
Everything worked fine yesterday, but now as i tried to use a SELECT command in the insert into statement to get things more dynamic i'm getting the above described syntax error.
Does anybody know what's wrong with this statement:
INSERT INTO [order]
(klant_id,totaalprijs,btw,subtotaal,verzendkosten)
SELECT klant.id
, SUM(orderregel.totaalprijs)
, SUM(orderregel.btw)
, SUM(orderregel.totaalprijs) - SUM(orderregel.btw)
, 7.50
FROM orderregel
INNER JOIN
klant
ON [order].klant_id = klant.id
WHERE klant.username = 'jerry'
GROUP BY
id;
the ordernumber in the "order" table is on autonumber, in the asp codebehind there is a for each which handles the lines being written for every product, there's an index set on 0 outside of this loop and is heightened with 1 every end of it. The executenonquery of the order is only executed once at the beginning of the first loop and the lines are added after with MAX(ordernumber) as ordernumber.
I hope i have provided enough information and somebody is capable of helping me.
Thanks in advance!
EDIT:
thanks everybody, using this query did it!
INSERT INTO [order]
(klant_id,totaalprijs,btw,subtotaal,verzendkosten) SELECT (SELECT klant.id FROM klant WHERE klant.username = 'jerry') ,
SUM(orderregel.totaalprijs) , SUM(orderregel.btw) ,
SUM(orderregel.totaalprijs) - SUM(orderregel.btw) , 7.50 FROM
orderregel;
You have used [order] in a JOIN, when it should be orderregel I guess.
FROM orderregel
INNER JOIN klant ON [order].klant_id = klant.id
should be:
FROM orderregel
INNER JOIN klant ON orderregel.klant_id = klant.id
Edit:
Why not just using:
INSERT INTO [order]
(klant_id,totaalprijs,btw,subtotaal,verzendkosten)
SELECT (SELECT klant.id FROM klant WHERE klant.username = 'jerry')
, SUM(orderregel.totaalprijs)
,...
... and avoid JOIN with klant table?
You can't refer to the table being inserted into. After all, those rows aren't yet there before the insert completes!
Reading your query, it's clear that you're trying to insert the klant called Jerry. But how do you specify which orderlines are used for the insert?
A possible solution:
Write the order first, with the klant id
Create the order lines. You know the orderid from the first query (f.e. using select SCOPE_IDENTITY()
Update the order with totals
Try this:
INSERT INTO [order]
(klant_id,totaalprijs,btw,subtotaal,verzendkosten)
SELECT klant.id
, SUM(orderregel.totaalprijs)
, SUM(orderregel.btw)
, SUM(orderregel.totaalprijs) - SUM(orderregel.btw)
, 7.50
FROM orderregel
INNER JOIN klant ON orderregel.id = klant.id
INNER JOIN [order] ON [order].klant_id = klant.id
WHERE klant.username = 'jerry'
GROUP BY id;
My situation is after login my website I wanted to show how many employees active, inactive & also for department wise, collage wise employees list of counts.
For that I created a procedure to create temporary table if it is not exist else drop table create temporary table, after that I wrote some SQL queries to get count of employees, department with conditions & then I'm inserting records to table.
Then I need the inserted rows. Now my problem is while executing procedure in SQL it executes but it's not creating & inserting any rows, I don't know why this happens. Please help me if any knows a solution to this problem.
My code:
alter proc SP_TEMPRECORDFORCOUNT
as
begin
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'TEMPRECORDFORCOUNT'))
BEGIN
drop table dbo.TEMPRECORDFORCOUNT
END
create table dbo.TEMPRECORDFORCOUNT(totalemployeecount int,activeemployeecount int,inactiveemployeecount int,deptwiseemployeecount int,activeemployeecount int)
declare #totalemployeecount int
declare #activeemployeecount int
declare #inactiveemployeecount int
declare #deptwiseemployeecount int
declare #activeemployeecount int
select #totalemployeecount =COUNT(*) from Employee
select #activeemployeecount =COUNT(*) from Employee where status=1
select #inactiveemployeecount =COUNT(*) from Employee where status=0
select #deptwiseemployeecount = count(*) from Department where e_id !=null
select #activeemployeecount = count(*) from Department d inner join Employee e on d.e_id =e.e_id where status_id=1
insert into TEMPRECORDFORCOUNT
(
totalemployeecount
,activeemployeecount
,inactiveemployeecount
,deptwiseemployeecount
,activeemployeecount
)
values
(
#totalemployeecount ,
#activeemployeecount ,
#inactiveemployeecount ,
#deptwiseemployeecount ,
#activeemployeecount ,
)
end
is it correct way thing i'm doing? if not please correct me.
With SQL Server 2008 R2 you have the option for table variables.
So your statement should be changed to the following:
DECLARE #TEMPRECORDFORCOUNT TABLE(totalemployeecount int,activeemployeecount int,inactiveemployeecount int,deptwiseemployeecount int,activeemployeecount int)
insert into #TEMPRECORDFORCOUNT
(
totalemployeecount
,activeemployeecount
,inactiveemployeecount
,deptwiseemployeecount
,activeemployeecount
)
values
(
#totalemployeecount ,
#activeemployeecount ,
#inactiveemployeecount ,
#deptwiseemployeecount ,
#activeemployeecount ,
)
SELECT * FROM #TEMPRECORDFORCOUNT
;
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))
I'm trying to combine an insert sql statement with a select statement, I successfully combined this statement:
insert into dbo.sessions (se_user) select user_id from dbo.users where username = 'bader'", badersql);
What I need now is to insert another value in the same sql statement, like this:
insert into dbo.sessions (se_user,*se_ip*) select user_id from dbo.users...
As you notice there is se_ip here in the second statement, the value will be from a text box, how can I combine it with my sql statement?
INSERT INTO dbo.sessions (se_user, se_ip)
SELECT user_id, #ipAddress FROM dbo.users WHERE username = #username
You can select constant from a parameter:
using (var command = new SqlCommand(#"insert into dbo.sessions (se_user, se_ip)
select user_id, #text from dbo.users...", connection) {
command.Parameters.AddWithValue("text", myTextBox.Text);
command.ExecuteNonQuery();
}
insert into dbo.sessions
(se_user,se_ip)
select user_id, #TextBoxValue
from dbo.users...
You can use the first insert statement that you have mentioned in your question and then add an update statement to modify the data as you need to
OR
I am assuming you have the value of the textbox in a variable and so you can use something like SELECT user_id,#yourtextboxvalue from userstable...
You'll need to add another field to the select portion of your insert statement
insert into dbo.sessions (se_user,*se_ip*) select user_id, other_value from dbo.users ...
If the value comes from a text box,, it depends on how you're running the insert statement. You'll probably have to set a parameter to your command object. see this link for more details.