在SQL Server中使用多个参数创建和执行函数

时间:2016-11-22 09:43:40

标签: sql-server tsql sql-server-2012

我有四张桌子:

1-dbo.Projects(ID,项目名,地区,PaymentSystem,location.id,purpose.id,types.id等)

2-dbo.Locations(id,location name)

3-dbo.Purpose(id,Purposename)

4-dbo.Types(id,typname)

我有一个搜索条件,这个标准填充了数据库表中的数据:位置,目的和类型,还有最大值和最小值的文本框。

我会搜索所有字段以及最大值,最小值 - 我已达到此查询但错过了查询以获取或过滤最大值和最小值:

ALTER FUNCTION SearchProjects (
@location NVARCHAR(50),
@purpose NVARCHAR(50),
@type NVARCHAR(50),
@max nvarchar(50),
@min nvarchar(50))
RETURNS TABLE
AS
  RETURN
  (
  SELECT  p.ProjectName, 
        p.Areas, 
        p.PaymentSystem, 
        p.ReceivedDate,    
        p.PropertyClassification, 
        p.ProjectImage,         
        l.LocationName,
        Pur.PurposeName,            
        t.TypeName
FROM dbo.Projects AS p 
LEFT JOIN dbo.Locations AS l ON p.LocationID = l.ID      
LEFT JOIN dbo.Purpose pur ON p.PurposeID = pur.ID 
LEFT JOIN dbo.[Types] AS t ON p.TypeID = t.ID
WHERE UPPER(ISNULL(l.LocationName,N'')) LIKE N'%' + UPPER(@location) + '%'
AND UPPER(ISNULL(pur.PurposeName,N'')) LIKE N'%' + UPPER(@purpose) + '%'
AND UPPER(ISNULL(t.TypeName,N'')) LIKE N'%' + UPPER(@type) + '%'
AND p.Areas like @max
AND p.Areas like @min
 )
 GO
SELECT * FROM dbo.SearchProjects('','','','200','250');

1 个答案:

答案 0 :(得分:1)

根据我的理解,您希望过滤area列的maxminAND p.Areas BETWEEN @min AND @max 值,您可以使用BETWEEN

WHERE

因此,您的WHERE UPPER(ISNULL(l.LocationName,N'')) LIKE N'%' + UPPER(@location) + '%' AND UPPER(ISNULL(pur.PurposeName,N'')) LIKE N'%' + UPPER(@purpose) + '%' AND UPPER(ISNULL(t.TypeName,N'')) LIKE N'%' + UPPER(@type) + '%' AND p.Areas BETWEEN @min AND @max 条件看起来像

AzureCredentials credentials = AzureCredentials.FromFile("Full path of your AzureAuthFile");
                var azure = Azure
                    .Configure()
                    .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC)
                    .Authenticate(credentials)
                    .WithDefaultSubscription();
  foreach (var virtualMachine in azure.VirtualMachines.ListByGroup("Your Resource Group Name").Where(virtualMachine => virtualMachine.ComputerName.Equals("vmName")))
                    {
                        var state = virtualMachine.PowerState;
                        var size = virtualMachine.Size;
                        var ip = virtualMachine.GetPrimaryPublicIpAddress().IpAddress; //call Rest API again
                    }