我有4张桌子
table1
(id, stateId(fk), name, carId(fk)
)table2
(stateId(pk), state, countryId(fk)
)table3
(countryId, country, currency
)table4
(carId, car
)我想通过使用连接的存储过程从上表中检索名称,状态,国家/地区,汽车。如果其他一些简单的方法,请告诉。
感谢。
答案 0 :(得分:0)
一个简单的JOIN
应该:
CREATE PROCEDURE schema.YourStoredProcedureName
AS
SELECT t1.name, t2.state, t3.country, t4.car
FROM table1 t1
JOIN table2 t2 ON t1.stateId = t2.stateId
JOIN table3 t3 ON t2.countryId = t3.countryId
JOIN table4 t4 ON t1.carId = t4.carId
GO
答案 1 :(得分:0)
没有其他简单的方法。加入是基本而简单的(http://msdn.microsoft.com/en-us/library/ms191517(v=sql.105).aspx)
select tbl1.name, tbl2.state, tbl3.country, tbl4.car
From table1 tbl1
inner join table2 tbl2 on tbl1.stateid =tbl2.stateid
inner join table3 tbl3 on tbl2.countryid = tbl3.countryid
inner join table4 tbl4 on tbl1.carId = tbl4.carId