在sql server中動態sql join

[英]Dynamic sql join in sql server


I have a relationship between two tables. The two tables PKs are int types.

我有兩個表之間的關系。兩個表PK是int類型。

In one table (UserS), I need to supply the Username and get the corresponding ID (which is the PK). This is the standard ASP.NET user table when using forms authentication.

在一個表(UserS)中,我需要提供用戶名並獲取相應的ID(即PK)。這是使用表單身份驗證時的標准ASP.NET用戶表。

However, in a related table, I want to supply the ID I find from the Users table to get a value out.

但是,在相關表中,我想提供我從Users表中找到的ID以獲取值。

Something like:

Run query to get ID for a username (simple select/where query) Return the result Run a subquery where I can pass in the above result - Get value where ID = result

運行查詢以獲取用戶名的ID(簡單選擇/其中查詢)返回結果運行子查詢,我可以傳入上述結果 - 獲取ID =結果的值

This sounds a lot like dynamic sql. However, there might be a better suited and appropriate way of writing this query (on Sql Server 2k5).

這聽起來很像動態sql。但是,可能有更適合和適當的方式來編寫此查詢(在Sql Server 2k5上)。

How can I go about doing this and what gotchas lurk?

我怎樣才能做到這一點以及潛伏着什么?

EDIT: I will try something along the lines of http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1

編輯:我將嘗試http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1的內容

EDIT: Thanks for the tips everyone, I wrote this:

編輯:感謝大家的提示,我寫道:

SELECT Discount.DiscountAmount FROM Discount INNER JOIN aspnet_Users ON Discount.DiscountCode = aspnet_Users.UserId And aspnet_Users.Username = 's'

SELECT Discount.DiscountAmount FROM Discount INNER JOIN aspnet_Users ON Discount.DiscountCode = aspnet_Users.UserId and aspnet_Users.Username ='s'

Where 's' is to be replaced by a parameter.

's'將被參數替換。

Thanks

6 个解决方案

#1


Right, i just would do this:

對,我就是這樣做的:

SELECT *
FROM TableB
JOIN Users ON Users.Id = TableB.ID 
WHERE Users.Username = @Username

#2


You don't have to use dynamic SQL for that.

您不必為此使用動態SQL。

You can use a lookup instead:

您可以使用查找:

DECLARE @ID bigint
SELECT @ID = ID FROM Users WHERE Username = @Username

SELECT
  *
FROM
  TableB
WHERE
  ID = @ID

Then, you could add the PARAMETER @Username to your SqlCommand object, preventing the risks of SQL Injection.

然后,您可以將PARAMETER @Username添加到SqlCommand對象,以防止SQL注入的風險。

Also, doing the lookup is preferable to a join, since the index is scanned a single time, for TableB.

此外,進行查找比連接更可取,因為對於TableB,索引會被掃描一次。

#3


Regarding lookup vs joins - while it may seem more intuitive for the novice to use the lookup, you should go with a join. A lookup needs to be evaluated for every row in your primary result set, while a join is evaluated once. Joins are much more efficient, which means that they are faster.

關於查找與連接 - 雖然新手使用查找似乎更直觀,但您應該使用連接。需要為主結果集中的每一行評估查找,而連接評估一次。連接效率更高,這意味着它們更快。

#4


This is just a simple join isn't it?

這只是一個簡單的連接不是嗎?

SELECT  x.*
FROM    user_table u
        INNER JOIN
                other_table x
                ON u.id = x.user_id
WHERE   u.name = @user_name

#5


SELECT  values.value
FROM    form_users, values
WHERE   form_users.username = 'John Doe'
        AND values.user = form_users.userid

#6


SELECT
   *
FROM
   table2 t2
   JOIN
   UserS t1 ON t2.IDKey = t1.IDKey
WHERE
   UserS.Username = @Input

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/07/725519e7dc829d3fc677d35bbe1c218a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com