SQL query to insert FIRST row on a table - c#

I have a SQL table with 10 columns, and two of the columns are project and employee.
What I am attempting to do is I have a drop down list of values for
the employee field in this table,
And I need to do an INSERT statement into the same table, inserting
all of the values that exist in the table, and just change the value
of the employee field on inserting.
I only want to insert a single record, so I am fearing that if I
execute an insert statement with on a WHERE project equals what the
user selected, that I will get thousands of rows inserted.
I just want to insert one single row from the table where the project
field is equal to what the user selected in a drop down, and insert
one single row copying all the data of a single row of that project,
and
Just replace the employee field with what the user selected in a
drop down.

You can INSERT using SELECT TOP 1 to get a row from the existing table with a WHERE clause that includes the required Project, like so:
DECLARE #EmployeeIdToAdd int = 12345
DECLARE #ProjectId int = 10
INSERT INTO [ProjectEmployees] (Col1, Col2,..., Project, Employee)
SELECT TOP 1 Col1, Col2,..., Project, #EmployeeIdToAdd
FROM ProjectEmployees
WHERE Project = #ProjectId
You would just need to set the parameters accordingly.

Related

How do I add 1 to a value after it's inserted into SQL Server table in C#?

Especially for the columns with ID's, I want to add a value of 1 after I inserted a ID into a SQL Server table, so it will be ID +1. Now I can't figure out a way to do this. Does anyone have tips?
In the table creation script, set that column as identity, so you don't need to mention the column name in the insert script, and the SQL server will automatically fill its value with the maximum value +1.
Here is an example of table creation and how to insert some data into it, and what the result will be.
Creation
create table tablename (id int identity,name nvarchar(max))
Insertion
Insert Into TableName(name) values('Test1')
Insert Into TableName(name) values('Test2')
Result
ID Name
1 Test1
2 Test2

SqlTableAdapter.Insert() with fewer Parameters

I have a table Users with 3 columns Id, Name, Address.
I am using usersTableAdapter.Insert() for adding a new record, but it requires all 3 parameters including Id which is Identity column and auto-incremented and cannot be added manually.
Here is the code:
this.usersTableAdapter.Insert("Haroon", "Pakistan");
This is not working.
Is there any way to use the above command without providing an Id value?
Yes; you've set your tableadapter up incorrectly..
Examine this screenshot of a tableadapter conencted to a sql server table that has an autoincrement ID column. You can see the table design from SSMS in the to right (blue border). You can see highlighted that the table adapter was created with select * from autoinctable and the wizard has ONLY chosen to reference the Text column for INSERT (also highlighted)
In code, the Insert command only asks for the Text column:
This TA was created with "refresh the dataset" turned on; you can see in the commands (like insert) that the database calculated value is retrieved (select ... scope_identity)

How to reorder auto increment column values, after deleting any row other than last row?

I want to know is there any SQL query for asp.net,c# that can just re arrange auto increment coloumn values..
eg.
deleting 2 in the table:
sno
1
2
3
4
does:
sno
1
3
4
but i want re-arrangement:
sno
1
2
3
Note:
Don't want to to the numbering manually
query to create table is like this:
CREATE TABLE uid (sno int IDENTITY(1,1) PRIMARY KEY, qpname nvarchar(500), mob int, tm int)
Let your table be named Parent and table that will hold the backup is called Backup. They should have identical columns.
INSERT INTO dbo.Backup
SELECT * FROM dbo.Parent
Now truncate the parent table
TRUNCATE TABLE dbo.Parent
Now you can just insert the data back using the first command and just reversing the table names.
Remember that this may not work in all cases. You may have On delete cascade on and if that is the case, then you would loose all data from other tables also which are referencing the parent table. I think that you should never use this is you are using any Foriegn Key reference on this table.
Following are the queries which should run 1 after other to get this functionality done. This can be easily achieved in C# by executing a generic ExecuteNonQuery().
DELETE FROM TBL1 WHERE sno = #sno;
UPDATE TBL1
SET sno = sno -1
WHERE sno > #sno;

How to get Id from MySQL autoincrement it in C# and Save it

I've made a form to save data in C#, what I want to do is get the last Id from my MySQL table, and print it but incremented by 1 in a TextBox, then when I save the entire formulary with the other data save the Id printed in the TextBox.
You can either select the ID and add 1 in the SQL
INSERT INTO TABLE (Field1) Values (value1); SELECT LAST_INSERT_ID() + 1;
Or select the id then add it in the c# code
INSERT INTO TABLE (Field1) Values (value1); SELECT LAST_INSERT_ID();
See here for more details
Select MAX(IdSocio) + 1 FROM Socios
Will give you the next ID but this is risky and not recommended. You cannot be 100% sure of the ID until after it is saved.

How to insert new record for multiple states

I have a table which holds various State information. There is redundancy in the table but it's on a production server so I don't have access to make changes.
This is an example of the data in the table:
Is there a way to insert a new row for each of the States in the table? The same information would be going into each row, the only difference would be the state name. So on the table example above, there are 3 states so 3 new rows need to be added..
maybe..
INSERT INTO myTable
(State,Shop,Platform,Additional)
Select distinct
State,
'info1',
'info2',
'info3'
from myTable
Will return one row for each State and insert all with the same information
You can just select distinct State from your table and insert them and additional values back to table like this:
insert into your_Table (State, Shop, Platform, Additional_Information)
select distinct State, #Shop_To_Insert, #Platform_To_Insert, #Information
from your_Table

Categories

Resources