English 中文(简体)
如何将人造数据列入一个选择说明
原标题:How to insert artificial data into a select statement
  • 时间:2010-03-12 08:47:34
  •  标签:
  • sql-server

回答如下:

SELECT A, B, C FROM tblA

Now, I need to additional artificial rows like SELECT Kyala , B, C FROM tblA when, for example, C = 100 to be inserted into the resultset.

例如,如果拖拉机有一个行:

A      B      C
John   1      100

我的目标是把以下两行带回一个单方的QL。

A      B      C
John   1      100
Kyala  1      100

我如何能够做到这一点<>,使用单一 不要依赖表变量或图表?

最佳回答

您可以通过http://msdn.microsoft.com/en-us/library/ms180026.aspx” rel=“nofollow noreferer”>UNION的操作员来做到这一点。

SELECT A, B, C from tblA
UNION
SELECT  Kyala , B, C FROM tblA WHERE C = 100

针对关于改进业绩的评论中的问题,只有一次问答表,你可以在C栏和B栏中增加一个覆盖指数,以便查询的第二部分使用该指数,而不是询问表格:

CREATE NONCLUSTERED INDEX [IX_tblA_CD] ON [dbo].[tblA] 
(
    [C] ASC
)
INCLUDE ( [B]) ON [PRIMARY]
GO

然而,根据使用情况(这种声音像某种特殊测试过程吗? ),你可能更喜欢把两张桌子的扫描带上,而不是增加可能不适合生产的新指数。

问题回答

简单地完善了解决欧盟错误的问题:

SELECT A, B, C from tblA
UNION
SELECT  Kyala  as A, B, C FROM tblA WHERE C = 100

如果你不想要其他人,那么你就能够这样做了(从工会中的第一个选择开始),那么你可以这样做:

SELECT A, B, C from tblA WHERE C <> 100
UNION
SELECT  Kyala , B, C FROM tblA WHERE C = 100

SELECT CASE(C)
           when 100 then  Kyala 
           else A
       END as A, B, C from tblA

您可使用化学文摘社:

SELECT B, C, 
       CASE 
          WHEN C = 100 THEN  Kyala 
          ELSE A
       END
FROM tblA

您可使用联合国协会的声明:

SELECT A, B, C FROM tblA
UNION
SELECT  Kyala , B, C FROM tblA WHERE C = 100




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签