SQL INNER JOIN,@Parameter - 不过滤结果
SQL INNER JOIN, @Parameter - does not filter results
提前感谢您能给我的任何建议。
我想要实现的目标:我有一个组合框,我想用 table1 / columnC 填充,按 table1 / columnB 和 table2 / columnB 中的匹配项排序。
例如:我想显示所有 Roberts 土豆,而不是每个人的土豆。
我为此创建了一个存储过程。目前看起来像这样:
CREATE PROCEDURE [dbo].[myProcedure]
@myParameter nvarchar
AS
SELECT columnA, columnB, columnC
FROM table1
INNER JOIN table2 ON table1.columnB = table2.columnB
WHERE table1.columnB = @myParameter
数据集:
DataSet MyDataSet()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlDataAdapter da = new SqlDataAdapater("myProcedure", con))
{
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//the line below gives value to my parameter, which in this case is a WinForms label
//with the table1/columnB, table2/columnB value (the name that I wish to sort by)
da.SelectCommand.Parameters.Add("@myParameter", SqlDbType.NVarChar).Value = label.Text.ToString();
da.Fill(ds);
}
return ds;
}
正在填充我的组合框(代码在表单加载事件中):
try
{
DataSet ds2 = MyDataSet();
myComboBox.DataSource = ds2.Tables[0];
myComboBox.DisplayMember = "columnC";
myComboBox.ValueMember = "columnA"; // hidden ID row
}
catch (Exception ex)
{
// messageboxcode
}
哦,而且,如果重要的话,我的所有列都是 nvarchar,除了 table1/columnA,它是 int。
希望我说得有道理,希望有人能给我一两个提示。
作为 SQL 的新手,关于我尝试解决此问题的方法,我阅读了文档,观看了教程并且通常花费数小时试图解决这个问题。我只是出于某种原因不能! :-)
我可以说您的存储过程格式不正确,因为您在 nvarchar()
上没有长度。 切勿在没有长度参数的情况下在 SQL 服务器中使用 char
和相关类型。。默认值因上下文而异,假设默认值执行您想要的操作只会导致难以调试错误(咳咳)。
在没有其他信息的情况下,你可以使用(max)
,但我建议你对参数的长度使用一个合适的值:
CREATE PROCEDURE [dbo].myProcedure (
@myParameter nvarchar(max)
) AS
BEGIN
SELECT columnA, columnB, columnC
FROM table1 JOIN
table2
ON table1.columnB = table2.columnB
WHERE table1.columnB = @myParameter;
END;
添加顺序如下:
order by table1.columnB,table2.columnB
提前感谢您能给我的任何建议。
我想要实现的目标:我有一个组合框,我想用 table1 / columnC 填充,按 table1 / columnB 和 table2 / columnB 中的匹配项排序。
例如:我想显示所有 Roberts 土豆,而不是每个人的土豆。
我为此创建了一个存储过程。目前看起来像这样:
CREATE PROCEDURE [dbo].[myProcedure]
@myParameter nvarchar
AS
SELECT columnA, columnB, columnC
FROM table1
INNER JOIN table2 ON table1.columnB = table2.columnB
WHERE table1.columnB = @myParameter
数据集:
DataSet MyDataSet()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlDataAdapter da = new SqlDataAdapater("myProcedure", con))
{
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//the line below gives value to my parameter, which in this case is a WinForms label
//with the table1/columnB, table2/columnB value (the name that I wish to sort by)
da.SelectCommand.Parameters.Add("@myParameter", SqlDbType.NVarChar).Value = label.Text.ToString();
da.Fill(ds);
}
return ds;
}
正在填充我的组合框(代码在表单加载事件中):
try
{
DataSet ds2 = MyDataSet();
myComboBox.DataSource = ds2.Tables[0];
myComboBox.DisplayMember = "columnC";
myComboBox.ValueMember = "columnA"; // hidden ID row
}
catch (Exception ex)
{
// messageboxcode
}
哦,而且,如果重要的话,我的所有列都是 nvarchar,除了 table1/columnA,它是 int。
希望我说得有道理,希望有人能给我一两个提示。 作为 SQL 的新手,关于我尝试解决此问题的方法,我阅读了文档,观看了教程并且通常花费数小时试图解决这个问题。我只是出于某种原因不能! :-)
我可以说您的存储过程格式不正确,因为您在 nvarchar()
上没有长度。 切勿在没有长度参数的情况下在 SQL 服务器中使用 char
和相关类型。。默认值因上下文而异,假设默认值执行您想要的操作只会导致难以调试错误(咳咳)。
在没有其他信息的情况下,你可以使用(max)
,但我建议你对参数的长度使用一个合适的值:
CREATE PROCEDURE [dbo].myProcedure (
@myParameter nvarchar(max)
) AS
BEGIN
SELECT columnA, columnB, columnC
FROM table1 JOIN
table2
ON table1.columnB = table2.columnB
WHERE table1.columnB = @myParameter;
END;
添加顺序如下:
order by table1.columnB,table2.columnB