创建 SQL 语句以同时使用 SSMS 和 SSRS

发布于 2024-09-12 19:55:12 字数 2788 浏览 2 评论 0原文

我需要暂时提出这个问题。假设您已经创建了一个名为 ParameterTesting 的数据库,并创建了一个表并按如下方式填充它:

IF EXISTS
     (SELECT 1
      FROM INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME = 'StarWars'
      AND   TABLE_SCHEMA = 'dbo')
  BEGIN
    DROP TABLE dbo.StarWars
    PRINT 'Dropped StarWars'
  END



SET  ANSI_NULLS ON;
GO
SET  QUOTED_IDENTIFIER ON;
GO

CREATE TABLE [dbo].[StarWars]
(
  [IdentityColumn]     INT IDENTITY (1, 1) NOT NULL
 ,[MasterDate]         DATETIME NULL
 ,[ForcePowerRating]   INT NULL
 ,[Name]               VARCHAR(25) NULL
 ,[Status]             VARCHAR(25) NULL
)
ON [PRIMARY];
GO


INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[Status])
VALUES (
         '5/21/1980'
        ,500
        ,'Luke Skywalker'
        ,'Master')

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         '1/1/1910'
        ,1000
        ,'Yoda'
        ,'Jedi Council')

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         '1/1/1930'
        ,NULL
        ,'Obi-Wan Kenobi'
        ,'Master')

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         '1/1/1920'
        ,300
        ,NULL
        ,NULL)

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         NULL
        ,450
        ,'Anakin Skywalker'
        ,'Sith Apprentice')

在本示例中,我尝试为 Status 创建一个多值参数,但这会变得很棘手,因为 Status 列可能为空。如果我不必担心空值,我可以简单地设置同一查询中的可用值和默认值,并且参数变为可选。

在我看来,在 SSMS 或类似工具中构建查询的正确方法是:

DECLARE @Status AS VARCHAR(20)
SET @status = 'Master'

SELECT [Name]
      ,ForcePowerRating
      ,MasterDate
      ,Status
FROM StarWars
WHERE 
(status in (@Status) or (status is null and   '..None' in (@Status)))

我将填充可用值和默认值的数据集:

select distinct coalesce(status, '..None') JediStatus from StarWars

我使用 IN 语句,以便它可以支持多个值。我的参数数据集查询用“..None”替换空值,并且此语句的 WHERE 子句处理空值。如果不这样做,您的参数将不会默认为 SSRS 中的整个列表,因为多个值参数不能支持 SSRS 中的 null。

本质上,我的问题是这样的。 在 SSMS 或其他工具中使用查询时,如何使用查询来填充 t-sql 变量?

我希望能够在 SSMS 中运行查询(包括参数列表查询)来检查我的工作。而不是:

Set @Status = ‘Master’

我想做类似的事情:

Set @Status = select distinct coalesce(status, '..None') JediStatus from StarWars

这行不通,使用表变量也行不通,因为你不能将 IN 与表变量一起使用。我研究过使用 SP 创建一串字符(来自查询),例如“Master”、“Jedi Council”、“..None”…………

这非常复杂,因为我必须支持 SQL 2000 并且不能使用 Common Table表达式。

那么,我有什么选择呢?我的想法正确吗?

I need to set up this question a moment. Let's say you've created a database called ParameterTesting and created a table and populated it like so:

IF EXISTS
     (SELECT 1
      FROM INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME = 'StarWars'
      AND   TABLE_SCHEMA = 'dbo')
  BEGIN
    DROP TABLE dbo.StarWars
    PRINT 'Dropped StarWars'
  END



SET  ANSI_NULLS ON;
GO
SET  QUOTED_IDENTIFIER ON;
GO

CREATE TABLE [dbo].[StarWars]
(
  [IdentityColumn]     INT IDENTITY (1, 1) NOT NULL
 ,[MasterDate]         DATETIME NULL
 ,[ForcePowerRating]   INT NULL
 ,[Name]               VARCHAR(25) NULL
 ,[Status]             VARCHAR(25) NULL
)
ON [PRIMARY];
GO


INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[Status])
VALUES (
         '5/21/1980'
        ,500
        ,'Luke Skywalker'
        ,'Master')

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         '1/1/1910'
        ,1000
        ,'Yoda'
        ,'Jedi Council')

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         '1/1/1930'
        ,NULL
        ,'Obi-Wan Kenobi'
        ,'Master')

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         '1/1/1920'
        ,300
        ,NULL
        ,NULL)

INSERT INTO StarWars(
              MasterDate
             ,ForcePowerRating
             ,[Name]
             ,[STATUS])
VALUES (
         NULL
        ,450
        ,'Anakin Skywalker'
        ,'Sith Apprentice')

In this example I am trying to create a multi-valued parameter for Status, but this gets tricky because the Status column can be null. If I didn't have to worry about nulls, I could simply set the available values and default values from the same query and the parameter becomes optional.

In my opinion the correct way to construct that Query in SSMS or a similar tool is:

DECLARE @Status AS VARCHAR(20)
SET @status = 'Master'

SELECT [Name]
      ,ForcePowerRating
      ,MasterDate
      ,Status
FROM StarWars
WHERE 
(status in (@Status) or (status is null and   '..None' in (@Status)))

I would populate the dataset for available and default values to:

select distinct coalesce(status, '..None') JediStatus from StarWars

I use the IN statement so it can support multiple values. My parameter dataset query substitutes ‘..None’ for nulls and the WHERE clause of this statement handles Null values. If you do not do so your parameter will not default to the entire list in SSRS because multiple value parameters cannot support nulls in SSRS.

Essentially, my problem is this. How can I use my query to populate my t-sql variable when working with it in SSMS or another tool?

I want to be able to run my queries (including the parameter list query) in SSMS to check my work. Instead of:

Set @Status = ‘Master’

I want to do something like:

Set @Status = select distinct coalesce(status, '..None') JediStatus from StarWars

That won’t work and using a table variable won’t either because you cannot use IN with a table variable. I looked into using a SP to create a string of characters (from a query) like ‘Master’,’Jedi Council’,’..None’………

That’s very complex because I have to support SQL 2000 and cannot use Common Table Expressions.

So, what are my options? Am I thinking about this correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-09-19 19:55:12

我认为你可以为此使用 ISNULL(,)

I think you can use an ISNULL(,) for this

海螺姑娘 2024-09-19 19:55:12

为什么不使用表变量并使用联接而不是 in 子句?

Why not use a table varaible and use a join instead of an in clause?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文