I have 2 table :
table1: name, family, cityID
table2: cityID, cityName
I have a gridview that it's datasource is:
select * from table1, table2 where table1.cityID = table2.cityID
I disable autoCreatColumns
I add 3 columns to my datagrid.
in first column I fix the data property name to name
in second column I fix the data property name to family
but in third colum when I fix the data propery name to table2.cityName It dosent work...
I want to show the city name in third column.
Its an example but in fact, I have cityName in anouther table that I used, and if I use just cityName, makes error
can any body help me?
Thanks...
try to change
select * from table1, table2 where table1.cityID = table2.cityID
To
select table1.name as Name,table1.family as Family,table2.cityName as City from table1, table2 where table1.cityID = table2.cityID
SELECT name,family,cityName from table1 INNER JOIN table2 ON table1.cityID = table2.cityID
As you are doing inner joins with city ids on both tables..U will get matched Table IDs with name,citynames
You don't need to write table2.cityName, just write cityName and it should work.
Related
Lets say I have two tables in database, Table1 and Table2. Relationship is one to many from table2 to table1. I would like to show all data from Table1 and not all columns from Table2 on GUI. Foreign key from table2 is column in Table1:
Table2 table2;
I am trying to display data from Table1 and some columns from Table2+foreign key. My problem is how to display foreign key, here is part of code:
List<Table1> list = new List<Table1>();
Table t2 = new Table2();
t2.prop1 = done using OledBReader;
t2.prop2 = ...;
t2.prop3 = ...;
Table1 t1 = new Table1();
t1.prop1 = ...;
t1.prop2 = ...;
*t1.Table2 = t2;*
list.Add(t1);
Two properties of Table2 are name and surname so I am overriding toString(), so that one column will be Name Surname. Foreign key is ID, but I dont know how to display It as a column (I dont want to include it in override method). My problem is that instead of that ID which is foreign key, I have property which is entire object of class Table2, and in Table2 I have to override name and surname, so I dont know where to include this ID, so I will have ID column at the end?
I hope this explanation is helpful.
I have the following records imported from excel to the database. The record count is 5,00,000.
EmpId Name City CityId
1 Ramesh LA ?
2 Kumar NewYork ?
I need to fetch the CityId from other table and insert into this CityId
The other table has the alias name for the cities and the cityId
CityId AliasName
1 LA
1 LosAngels
1 Los Angels
1 LA(USA)
I would like to call a stored procedure to update all 500000 records, as functions cannot be used for UPDATING record.
I need CityId field to be updated for each employee from Alias Table
you can do something like :
update employee
set cityid = b.cityid
from employee as a inner join city as b on a.city = b.aliasname
Hope this helps you out.
UPDATE UserDetails
SET UserDetails.CityID = City.ID
FROM City
WHERE City.AliasName = UserDetails.City
Would it not be easier to set the City as a foreign key rather than adding a redundant data column.
To answer your question, you can achieve this by running the following query. You can also make this into a stored procedure if it needs to be run occasionally.
UPDATE Employees SET CityId = (Select CityId FROM Cities where AliasName = City)
The following query will update the required data:
UPDATE Employees SET CityId = ISNULL((Select CityId FROM Cities where AliasName = City),0)
There are two tables:
Table1 : UserID Name Job
Table2 : BookID Book Car UserID
I load these two tables in one wpf datagrid:
da.SelectCommand = new SqlCommand("select Table1.UserID, Table1.Name, Table1.Job, Table2.Book, Table2.Car from Table1 inner join Table2 on Table1.UserID = Table2.UserID");
I want to delete one row from Table2 by DataGrid:
SqlCommand com = new SqlCommand("delete from Table2 where BookID=#BookID)",con);
but not work,
How can I do it?
You say "It must delete a book from a certain user not all books.".
You have to know user id for which the books must be delete.
If you want to delete users books, do this:
delete from Table2 where userid in (user_id1,user_id1, etc .....);
But you are large rows to delete, use bulk delete mechanism.
If you don't have user ids and have book ids, you have to do this:
Delete From Table2 where bookid in (book_id1,book_id2, etc ..);
Or Delete From Table2 where bookid =? //according your development language, you set "?" parameter. I don't know C# syntaxe.
You have to use the below way to delete the rows
DELETE FROM table2
WHERE userid = (SELECT userid
FROM table1);
Are you expecting something like this?
DELETE FROM B WHERE BOOKID IN (SELECT BOOKID FROM B,A WHERE B.USERID=A.USERID AND B.BOOK='ABCD');
i want get value of field after update,
for example i have two table
table1: ID Price UserID type
table2: IDLog ID(FK table1) OldPrice
and i update Price in table 1:
UPDATE table1
SET Price = '1111'
WHERE TYPE = 1
now i want befor every update get value ID table1 and insert into table2,
how i get value every field??
IMHO FOR UPDATE TRIGGER is the way to go. That way you can contain all logging logic inside your DB.
Better way to do it is using trigger on table1 (SQL Server side of course).
CREATE TRIGGER table1_update ON table1 FOR UPDATE
AS
BEGIN
INSERT INTO table2 (IDLog, OldPrice)
SELECT ID, Price
FROM deleted
INNER JOIN inserted ON
deleted.ID = inserted.ID
deleted.Price <> inserted.Price
END
You can use output clause:
update table1
output inserted.ID into table2
set Price='1111' where type=1
You can insert data to table2 before updating.
INSERT INTO table2
SELECT ID, Price from table1
WHERE TYPE = 1
--Update table1 after inserting
UPDATE table1
SET Price = '1111'
WHERE TYPE = 1
Other way to do is to create an update trigger for table1, which inserts the updated data to table2.
I have a gridview, when inserting data into the table dept(100, 1, 'IT') and when fetch the data from two tables emp and dept the records look like below grid:
EMpNo DeptNo DeptName
Harshal 1 IT
My problem is that, when I click on a grid view all records shows in respected text boxes
but issue is, I got all data except empno. Here I get as Name of employee instead of name I want to Empno.
you can try like this
select empno,deptno,deptname from emp inner join dept on emp.dept.id=dept.id