UpdateCommand for Asp GridView using a join between two tables - c#

I have two tables that I join sims and transactionlog and display in a Asp GridView using a SQLDatasource
Then I have an ASP GridView with the following select and update statements:
SelectCommand="SELECT * FROM sims s INNER JOIN TransactionLog AS tl ON s.id = tl.SimsId"
UpdateCommand="UPDATE SET s.Notes=#Notes FROM sims s INNER JOIN TransactionLog AS tl ON s.id = tl.SimsId WHERE s.id=#id and tl.Id=#Id1"
Right now I only have the Notes column not set to readonly, but would like to add the others in later as soon as I can get the update statement to work for the notes column first.
When I try to update the notes column the error I'm getting is the following:
Incorrect syntax near the keyword 'SET'.]
Mmm, hope my question is stated clearly enough.

This is how you write and SQL UPDATE statement with the FROM clause.
drop table t2
drop table t1
go
create table t1 (
Id int not null identity(1,1) primary key,
Name varchar(50) null
)
create table t2 (
t1Id int not null foreign key references t1(id),
Name varchar(50) null
)
insert into t1(name) values('T1Test1')
insert into t1(name) values('T1Test2')
insert into t2(t1Id, name) values(1, 'T2Test1')
insert into t2(t1Id, name) values(2, 'T2Test2')
-- Have a look at the data
select * from t1 inner join t2 on t1.Id = t2.t1Id
update t2
set t2.Name = 'T2Test1_changed'
from t2 inner join t1 on t2.t1Id = t1.Id
where t1.Id = 1
-- See the changes
select * from t1 inner join t2 on t1.Id = t2.t1Id
This means that your statement should look like this:
UPDATE sims
SET s.Notes = #Notes
FROM sims s INNER JOIN TransactionLog tl ON s.id = tl.SimsId
WHERE s.id = #id and tl.Id = #Id1
I do however doubt the WHERE clause. Maybe there's no need to use both IDs there, but only you know that.
Try you queries in SQL Management Studio before applying them in you application for a better debugging experience.
Here is the MSDN docs on the UPDATE statement: http://msdn.microsoft.com/en-us/library/ms177523.aspx

Related

Update one table using self-join for other table

I have two tables. I need to update the records of the 1st table performing a join with the 2nd table. But in the 2nd table I also have to put a self join.
UPDATE [T2]
SET T2.NAME = T1.NAME
FROM [TABLE2] T2
JOIN [TABLE1] T1 ON T2.ID = T1.ID
JOIN [TABLE1] T3 ON T1.RECORDTYPE = T3. RECORDTYPE
The purpose is to perform a self-join and update the other table.
Is this what you mean?
UPDATE T2
SET T2.NAME = T1.NAME
FROM T2
JOIN T1
ON T2.ID = T1.ID
AND T2.RECORDTYPE = T1.RECORDTYPE;

SQL - join three table with "where" clause

I want to write an SP that I will use in my c# program. I have three tables: Table1 (id pk, name, ...), Table2 (id PK, name, ...) and Table3((idT1,idT2) PK FK). So, Table3 models the n:n relationship between Table1 and Table2.
I want to retrieve all the Table2.name(s) related to a single Table1.name.
I have already tried to write a query with two inner join
CREATE PROCEDURE slct
#name nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT Table2.name from Table2
join Table3 on Table2.id = Table3.idT2 join Table1 on Table1.id = Table3.idT1 where Table1.name = '%'+#name+'%'
END
GO
Table1
|1|abc|
|2|def|
Table2
|1|xyz|
|2|mno|
Table3
|1|1|
|1|2|
|2|2|
As result, I see more records than are needed. I expected a list of Table2.name related to a single Table1.name (specified by #name parameter)
sorry for my english.
select Table2.Names from Table2
inner join
(select Table3.idT1 as SubQueryID1, Table3.idT2 as SubQueryID2
from Table3 inner join Table1 on Table1.id = Table3.idT1)
on
Table2.Id = SubQueryID2
SELECT
Table2.name
FROM Table2
INNER JOIN Table3 ON Table2.id = Table3.idT2
CROSS APPLY(
SELECT TOP 1 * FROM Table1 WHERE Table1.id = Table3.idT1
) t
WHERE
Table1.name = '%'+#name+'%'
I want to retrieve all the Table2.name(s) related to a single Table1.name.
select t2.name
from table2 t2 join
table3 t3
on t3.idT2 = t2.id
group by t2.name
having count(*) = 1;
This returns names in t2 that have only one row in table3. This makes two assumptions (both of which are reasonable for this data structure):
Names are unique in table1 and table2.
Pairs are unique in table3.
I want to retrieve all the Table2.name(s) related to a single Table1.name.
Your query actually has the right join logic. You have to clarify the scope of the where clause.
Table1.Name contains #name: you need the like operator instead of the equal operator.
Table1.Name strictly equals to #name: you have to remove the wildcards.
See below two examples:
select distinct T2.Name
from Table1 as T1
inner join Table3 as T3 on T3.IDT1 = T1.ID
inner join Table2 as T2 on T2.ID = T3.IDT2
where T1.Name like '%'+#name+'%'
or
select distinct T2.Name
from Table1 as T1
inner join Table3 as T3 on T3.IDT1 = T1.ID
inner join Table2 as T2 on T2.ID = T3.IDT2
where T1.Name = #name

Joined SELECT blocking UPDATE inside WHILE loop

Gets all fields from Table but only the ones created last, for each different Field
SELECT t1.*
FROM Table AS t1
INNER JOIN (
SELECT Field
, MAX(CreatedOn) AS MaxDate
FROM Table
WHERE ImportedOn IS NULL
AND Status <> 'error'
GROUP BY Field) AS t2
ON t1.Field = t2.Field
AND t1.CreatedOn = t2.MaxDate
I read this data inside a while loop. At the end I try to update the same table - the same record read - and it gives me a timeout. I figure its because the table is blocked for changes.
My question is: How to work around it? Any way to 'fix' the query so it'll accept updates?
Extra info: If I query without JOIN it works perfectly.
Instead of doing an UPDATE row by row after every read; do a JOIN with the table and update. Something like below [A sample; not the exact query]
UPDATE t1
SET column_name = tab.some_column_name
FROM Table t1
JOIN
(
SELECT t1.*
FROM Table AS t1
INNER JOIN (
SELECT Field,
MAX(CreatedOn) AS MaxDate
FROM Table
WHERE ImportedOn IS NULL
AND Status <> 'error'
GROUP BY Field
) AS t2
ON t1.Field = t2.Field
AND t1.CreatedOn = t2.MaxDate
) tab
ON t1.Field = tab.Field

Using COUNT For Comparison in SQL Server CE 4.0

I'm attempting to combine the logic for some of my SQL queries, and I can't seem to figure out this problem. Obviously SQL Server CE has many limitations compared to SQL Server or mySQL, but surely there's a way to solve this.
I want to do a count on one table in my database, based on some parameters, and then I want to compare this value to a value stored in a column in another table.
Let's say the database is modeled like this:
Table1:
ID int
Key string
NumberInUse int
Table2:
ID int
OtherID int
Here's the necessary parts of the query.
SELECT *
FROM Table1
LEFT JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table1.Key = #key
AND (SELECT COUNT(*) FROM Table2 WHERE ID = Table1.ID AND OtherID = #otherID) < Table1.NumberInUse;
Unfortunately this query gives me this error:
There was an error parsing the query. [ Token line number = 4,Token line offset = 6,Token in error = SELECT ]`
So is there a way I can rephrase the WHERE clause of my query to utilize this comparison?
Try this:
SELECT *
FROM Table1 t1
INNER JOIN (SELECT ID
,COUNT(*) numCount
FROM Table2 t2
WHERE t2.OtherId = #otherID
GROUP BY ID) t3
ON t1.ID = t3.ID
WHERE t1.Key = #Key
AND t3.numCount < t1.NumberInUse
Sure it's not SQL. You're missing the right operand of the second LEFT JOIN:
SELECT *
FROM Table1 LEFT JOIN Table2
ON Table1.ID = Table2.ID
LEFT JOIN ????? WHUT ?????
WHERE Table1.Key = #key
AND (SELECT COUNT(*) FROM Table2 WHERE ID = Table1.ID AND OtherID = #otherID) < Table1.NumberInUse;

How can i update another tables after adding record to a table in sql server 2008

I have two same tables created in SQL Server 2008.
What I need is:
Add the newest data inserted in table1 into table2.
Is that possible?
Something like services or triggers or stored procedures?
I think that I have to use triggers, but I don't know how.
I have these two tables:
table1 ( field1,field2,field3 )
table2 ( field1,field2,field3 )
when i insert into table1 i need table2 to be updated as the procedure below:
> IF NotExist(field1=value) in table2 THEN INSERT Into table2 ELSE UPDATE table2
> with new field1 value.
Here is an example:
USE DatabaseName;
GO
IF OBJECT_ID ('Sch.UpdateSecondTable', 'TR') IS NOT NULL
DROP TRIGGER Sch.UpdateSecondTable;
GO
CREATE TRIGGER Sch.UpdateSecondTable
ON FirstTable
AFTER INSERT
AS
INSERT INTO SecondTable
SELECT * FROM Inserted
GO
SQL Fiddle Demo
However, if you want to update the second table form with the inserted values from the first table, try this instead of the INSERT clause:
UPDATE t2
SET t2.name = t1.name
FROM SecondTable t2
INNER JOIN inserted t1 ON t1.id = t2.id
SQL Fiddle Demo
Update: If you want to insert only the values that doesn't exist in the second tabl3e. If these tables are of the same structures, i.e the same column numbers and the same data type. You can use the EXCEPT set operator to do this like so:
UPDATE t2
SET t2.name = t1.name
FROM SecondTable t2
INNER JOIN
(
SELECT * FROM inserted t1
EXCEPT SELECT * FROM SECONDTABLE
) t1 ON t1.id = t2.id
SQL Fiddle Demo
If not use the standard way to do this like so:
UPDATE t2
SET t2.name = t1.name
FROM SecondTable t2
INNER JOIN
(
SELECT * FROM inserted t1
WHERE id NOT IN(SELECT id FROM SECONDTABLE)
) t1 ON t1.id = t2.id
SQL Fiddle Demo
You need to use trigger, its not a big deal, do some research, you will get on the internet.

Categories