我有一个名为abc.cs
的班级。我正在使用自定义函数来搜索这样的标准:
public System.Data.DataTable myFunction(string SearchText, string ColumnName, string
SearchCriteria)
{
// ColumnName is the name of the column in db table as shown below in
// query e.g a , b , c , d, e
try
{
string strQuery = "SELECT a,b,c,d,e FROM myTable ";
SearchText = SearchText.Trim().Replace("[", "[[]");
SearchText = SearchText.Trim().Replace("'", "''");
SearchText = SearchText.Trim().Replace("%", "[%]");
SearchText = SearchText.Trim().Replace("_", "[_]");
if (SearchText != "")
{
strQuery += " where " + ColumnName + " LIKE ";
if (SearchCriteria == "x")
{
strQuery += "'" + SearchText + "%'";
}
else if (SearchCriteria == "y")
{
strQuery += "'%" + SearchText + "'";
}
else if (SearchCriteria == "z")
{
strQuery += "'%" + SearchText + "%'";
}
else
{
strQuery += "'" + SearchText + "'";
}
}
strQuery += "ORDER BY b";
}
catch (Exception E)
{
}
}
到目前为止我尝试过的商店程序:
USE [dbName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[abc]
@SearchText nvarchar(100)
AS
BEGIN
SELECT a,b,c,d,e FROM myTable ;
-- what should be the criteria here.
END
GO
我很难理解如何在商店流程中的条件中使用searchText
,以及在searchText
中使用myFunction
的方式是什么。
答案 0 :(得分:0)
你可以用这个......
USE [dbName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[abc]
@SearchText nvarchar(100)
AS
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql = 'SELECT a,b,c,d,e FROM myTable ' + @SearchText
-- what should be the criteria here.
EXEC sp_executesql @sql
END
GO