Is there a way to make a TSQL variable constant?

前端 未结 12 1047
别那么骄傲
别那么骄傲 2021-02-01 11:31

Is there a way to make a TSQL variable constant?

12条回答
  •  离开以前
    2021-02-01 12:10

    For enums or simple constants, a view with a single row has great performance and compile time checking / dependency tracking ( cause its a column name )

    See Jared Ko's blog post https://blogs.msdn.microsoft.com/sql_server_appendix_z/2013/09/16/sql-server-variables-parameters-or-literals-or-constants/

    create the view

     CREATE VIEW ShipMethods AS
     SELECT CAST(1 AS INT) AS [XRQ - TRUCK GROUND]
       ,CAST(2 AS INT) AS [ZY - EXPRESS]
       ,CAST(3 AS INT) AS [OVERSEAS - DELUXE]
      , CAST(4 AS INT) AS [OVERNIGHT J-FAST]
       ,CAST(5 AS INT) AS [CARGO TRANSPORT 5]
    

    use the view

    SELECT h.*
    FROM Sales.SalesOrderHeader 
    WHERE ShipMethodID = ( select [OVERNIGHT J-FAST] from ShipMethods  )
    

提交回复
热议问题