How to insert new record for multiple states - c#

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

Related

Update a field 'version' into a table

I have this situation:
I have two tables:
Table A
Staging_Table A
Both tables contain those common columns:
Code
Description
Into Table A I also have a column Version which identifies the last version of corresponding column Code.
My problem is how to update the column Version once a new Description is stored for the same Code (I fill up the Staging_Table with a bulk Insert from C#. I have a flow of data that change once a week).
I need to insert the new row into Table A which contain the same Code, but a different Description, without deleting the old one.
I insert the rows from Staging table to table A with MINUS operation and I have this mechanism within a stored procedure because I also fill up the staging table with a Bulk Insert from C#.
The result I need to obtain is the following:
TABLE A:
Id Code Description Version End_date
-- ----------------- ------- --------
1 8585 Red Car 1 26-mag-2015
2 8585 Red Car RRRR 2 01-giu-2015
How can I do that?
I hope the issue is clear
If I understand correctly process work like that:
1. Data is loaded to staging table Staging_table_A
2. Data is inserted from Staging_table_A itno Table_A with additional column version.
I would do:
with cnt as (select count(*) c, code from Table_A group by code)
Insert into Table_A (select sta.*, nvl(cnt.c,0) + 1 as version
from Staging_table_A sta left outer join cnt on (sta.code = cnt.code));
This is based on condition that in Table_A versions contains no duplicates.

How to insert values from a table in another along with other values in sql server?

I'm working on a project in which i need to insert data from one table to another along with another value, lets say I have table1(col1,col2,col3) and i have table2(col2) so i wrote the following:-
INSERT INTO table1 (col1,col2) VALUES ((#constant),(SELECT col2 FROM table2))
How to do that knowing that #Constant is a parameter that has a value used inside a stored procedure?
INSERT INTO table1 (col1,col2)
SELECT #constant, col2
FROM table2

SQL query to insert FIRST row on a table

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.

UPDATE TableA.FK VALUES TableB.PK WHERE TableA.Column = TableB.Column

I'm trying to merge tables from different databases, ServerDB and ClientDB and save to ClientDB where the client's tables hold the master's tables records plus any records the user might add. On the other hand server tables could also be updated (new records inserted).
The database has relationships. The column in TableA I want to INSERT INTO values is a ForeignKey linking to TableB's PrimaryKey which is an auto-incremental column.
I'm saving all records from both databases in a merged dataset, and due to database design restrictions, I need to clear both tables on the client before inserting the merged tables from the dataset.
I first update TableB (the one with the PrimaryKey auto-increment column), but now the values of this column have nothing to do with the ForeignKey on TableA, so I update TableA and temporary inserting in the ForeignKey column the value of the first record of TableB's PK. Now I need to update TableA foreignKey column with the correct values from TableA PK column. Theres also a third column on each table that have the same values.
What the syntax of the sql statement should be? If I don't make much sense let me know and I'll post a better description.
It was a confussing question but i think you are talking about inserting values from table 1 to table 2 where table 1 value is equal to table 2 values the sql query for this operation is
INSERT INTO emp (empno,ename)
SELECT t2.deptno,
t2.dname
FROM dept t2
LEFT JOIN emp t1
ON t2.deptno = t1.deptno
in this query table 1 (emp) will insert 2 values into columns(empno and ename) from
table 2 (dept) and join is on (deptno) which is present in both tables.
You can further ask if this was not helpful.
Thank you all for your help. After struggling for a while with the "OledbException Operation must use an updateable query" I found out a solution in a similar topic: SQL Update woes in MS Access - Operation must use an updateable query
Thats the query that did the trick:
UPDATE DISTINCTROW PlaylistsSongs
INNER JOIN PlaylistsNames ON PlaylistNames.PlaylistName = PlaylistsSongs.PlaylistName
SET PlaylistID = PlaylistNames.ID

How to get an AutoNumber from DataTable AutoIncrementColumn with a Strongly-Typed Database in c#?

I have added a strongly-typed Database (MySQl) to a C# (.net 3.5) project. The database (DB.Electrophysiology_Data) has several relational tables but basically I want to add a new 'filesRow' to the 'files' table. I use something like the following code.
DB.Electrophysiology_Data.filesRow new_file = ds.files.NewfilesRow();
...set the values for the new_file (eg, filename, creationdate etc)
ds.files.AddfilesRow(new_file);
...eventually I update the Database via a DataAdaptor
file_adaptor.Update(ds.files)
The problem is that the AutoIncrement column (FileID) is set to -1. After closing my applicaiton and restarting, I see the new filesRow is added to the Database ok and now has an appropriate FileID value (about 5000, since I have about this many rows already in the files DataTable).
Is there a way of knowing the FileID value set by the database using the Strong-typed Database objects (DataSet, DataTable etc).
Thanks for any help,
pete
In order for adaptor to retrieve an auto-generated ID from the database just after you inserted a new record, you need to perform SELECT.
In My SQL You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function
If the Configure Query Wizard in stong-typed datasets cannot generate a query for MySQL in proper way, you can edit INSERT command manually and add SELECT clause here
Usually if you the command looks like
insert my_table (field1, field2, ...) values (#field1, #field2, ...)
select id, field1, field2 ... from my_table where id = LAST_INSERT_ID()
As you see, we need to select not only ID but other fields too in order to get last values from the table (that may be different from values in the dataset if any defaults exist).

Categories

Resources