Is there a way to make a TSQL variable constant?

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

Is there a way to make a TSQL variable constant?

相关标签:
12条回答
  • 2021-02-01 12:08

    My workaround to missing constans is to give hints about the value to the optimizer.

    DECLARE @Constant INT = 123;
    
    SELECT * 
    FROM [some_relation] 
    WHERE [some_attribute] = @Constant
    OPTION( OPTIMIZE FOR (@Constant = 123))
    

    This tells the query compiler to treat the variable as if it was a constant when creating the execution plan. The down side is that you have to define the value twice.

    0 讨论(0)
  • 2021-02-01 12:09

    The best answer is from SQLMenace according to the requirement if that is to create a temporary constant for use within scripts, i.e. across multiple GO statements/batches.

    Just create the procedure in the tempdb then you have no impact on the target database.

    One practical example of this is a database create script which writes a control value at the end of the script containing the logical schema version. At the top of the file are some comments with change history etc... But in practice most developers will forget to scroll down and update the schema version at the bottom of the file.

    Using the above code allows a visible schema version constant to be defined at the top before the database script (copied from the generate scripts feature of SSMS) creates the database but used at the end. This is right in the face of the developer next to the change history and other comments, so they are very likely to update it.

    For example:

    use tempdb
    go
    create function dbo.MySchemaVersion()
    returns int
    as
    begin
        return 123
    end
    go
    
    use master
    go
    
    -- Big long database create script with multiple batches...
    print 'Creating database schema version ' + CAST(tempdb.dbo.MySchemaVersion() as NVARCHAR) + '...'
    go
    -- ...
    go
    -- ...
    go
    use MyDatabase
    go
    
    -- Update schema version with constant at end (not normally possible as GO puts
    -- local @variables out of scope)
    insert MyConfigTable values ('SchemaVersion', tempdb.dbo.MySchemaVersion())
    go
    
    -- Clean-up
    use tempdb
    drop function MySchemaVersion
    go
    
    0 讨论(0)
  • 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  )
    
    0 讨论(0)
  • 2021-02-01 12:11

    There is no built-in support for constants in T-SQL. You could use SQLMenace's approach to simulate it (though you can never be sure whether someone else has overwritten the function to return something else…), or possibly write a table containing constants, as suggested over here. Perhaps write a trigger that rolls back any changes to the ConstantValue column?

    0 讨论(0)
  • 2021-02-01 12:12

    Okay, lets see

    Constants are immutable values which are known at compile time and do not change for the life of the program

    that means you can never have a constant in SQL Server

    declare @myvalue as int
    set @myvalue = 5
    set @myvalue = 10--oops we just changed it
    

    the value just changed

    0 讨论(0)
  • 2021-02-01 12:16

    No, but you can create a function and hardcode it in there and use that.

    Here is an example:

    CREATE FUNCTION fnConstant()
    RETURNS INT
    AS
    BEGIN
        RETURN 2
    END
    GO
    
    SELECT dbo.fnConstant()
    
    0 讨论(0)
提交回复
热议问题