What is the equivalent of the Oracle “Dual” table in MS SqlServer?

流过昼夜 提交于 2019-12-18 13:52:20

问题


What is the equivalent of the Oracle "Dual" table in MS SqlServer?

This is my Select:

SELECT pCliente,
       'xxx.x.xxx.xx' AS Servidor,
       xxxx AS Extension,
       xxxx AS Grupo,
       xxxx AS Puerto
FROM DUAL;

回答1:


In sql-server, there is no dual you can simply do

SELECT pCliente,
       'xxx.x.xxx.xx' AS Servidor,
        xxxx AS Extension,
        xxxx AS Grupo,
        xxxx AS Puerto

However, if your problem is because you transfered some code from Oracle which reference to dual you can re-create the table :

CREATE TABLE DUAL
(
DUMMY VARCHAR(1)
)
GO
INSERT INTO DUAL (DUMMY)
VALUES ('X')
GO



回答2:


You do not need DUAL in mssql server

in oracle

select 'sample' from dual

is equal to

SELECT 'sample'

in sql server




回答3:


While you usually don't need a DUAL table in SQL Server as explained by Jean-François Savard, I have needed to emulate DUAL for syntactic reasons in the past. Here are three options:

Create a DUAL table or view

-- A table
SELECT 'X' AS DUMMY INTO DUAL;

-- A view
CREATE VIEW DUAL AS SELECT 'X' AS DUMMY;

Once created, you can use it just as in Oracle.

Use a common table expression or a derived table

If you just need DUAL for the scope of a single query, this might do as well:

-- Common table expression
WITH DUAL(DUMMY) AS (SELECT 'X')
SELECT * FROM DUAL

-- Derived table
SELECT *
FROM (
  SELECT 'X'
) DUAL(DUMMY)



回答4:


In SQL Server there is no dual table. If you want to put a WHERE clause, you can simple put it directly like this:

SELECT 123 WHERE 1<2

I think in MySQL and Oracle they need a FROM clause to use a WHERE clause.

SELECT 123 FROM DUAL WHERE 1<2



来源:https://stackoverflow.com/questions/28371342/what-is-the-equivalent-of-the-oracle-dual-table-in-ms-sqlserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!