簡體   English   中英

從SQL Server 2008中的表中檢索數據

[英]Retrieving data from the table in SQL Server 2008

我有4張桌子

  1. table1id, stateId(fk), name, carId(fk)
  2. table2stateId(pk), state, countryId(fk)
  3. table3countryId, country, currency
  4. table4carId, car

我想通過存儲過程使用聯接從上表中檢索名稱,州,國家,汽車。 如果還有其他簡單的方法,請告訴。

謝謝。

一個簡單的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

沒有其他簡單的方法。 加入是基本且簡單的( http://msdn.microsoft.com/zh-cn/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM