문제

TSQL 변수를 일정하게 만드는 방법이 있습니까?

도움이 되었습니까?

해결책

아니요.하지만 함수를 생성하고 거기에 하드 코딩하여 사용할 수 있습니다.

다음은 예입니다. 라코 디스

다른 팁

constant 누락에 대한 해결 방법은 옵티 마이저에 값에 대한 힌트를 제공하는 것입니다. 라코 디스

실행 계획을 생성 할 때 변수를 상수 인 것처럼 처리하도록 쿼리 컴파일러에 지시합니다.단점은 값을 두 번 정의해야한다는 것입니다.

의사 상수 사용 : http://blogs.msdn.com/b/sql_server_appendix_z/archive/2013/09/16/sql-server-variables-parameters-or-literals-or-constants.aspx <인용구>

의사 상수는 변수 나 매개 변수가 아닙니다.대신, 그들은 단순히 하나의 행과 충분한 열이있는보기 상수.이러한 간단한 규칙으로 SQL 엔진은 뷰의 가치를 기반으로 실행 계획을 구축합니다. 값.실행 계획은 뷰에 대한 조인도 표시하지 않습니다!

아니요. 그러나 좋은 오래된 명명 규칙을 사용해야합니다. 라코 디스

T-SQL에는 상수에 대한 기본 지원이 없습니다.SQLMenace의 접근 방식을 사용하여 시뮬레이션하거나 (다른 사람이 다른 것을 반환하기 위해 함수를 덮어 썼는지 확인할 수는 없지만…) 상수가 포함 된 테이블을 작성할 수 있습니다. 여기에서 제안한대로 .ConstantValue 열의 변경 사항을 롤백하는 트리거를 작성 하시겠습니까?

SQL 함수를 사용하기 전에 다음 스크립트를 실행하여 성능 차이를 확인하십시오. 라코 디스

변수의 값에 대한 최적의 실행 계획을 얻으려면 동적 SQL 코드를 사용할 수 있습니다.변수를 일정하게 만듭니다. 라코 디스

열거 형 또는 단순 상수의 경우 단일 행이있는 뷰는 뛰어난 성능과 컴파일 시간 검사 / 종속성 추적을 제공합니다 (열 이름이 원인이 됨)

Jared Ko의 블로그 게시물 https://blogs.msdn.microsoft.com/sql_server_appendix_z/2013/09/16/sql-server-variables-parameters-or-literals-or-constants/

보기 만들기 라코 디스

보기 사용 라코 디스

알겠습니다.

상수는 컴파일 타임에 알려지고 프로그램 수명 동안 변경되지 않는 불변의 값입니다.

즉, SQL Server에서 상수를 가질 수 없습니다. 라코 디스

값이 방금 변경됨

상수를 지원하는 빌드가 없기 때문에 내 솔루션은 매우 간단합니다.

지원되지 않으므로 : 라코 디스

간단히 변환 라코 디스

분명히 이것은 모든 것 (후행 공백과 주석이없는 값)이 고유하다는 점에 의존합니다.전역 검색 및 바꾸기로 변경할 수 있습니다.

데이터베이스 문헌에는 "상수 생성"과 같은 것이 없습니다.상수는있는 그대로 존재하며 종종 값이라고합니다.변수를 선언하고 여기에 값 (상수)을 할당 할 수 있습니다.학문적 관점에서 : 라코 디스

여기서 @two는 변수이고 2는 값 / 상수입니다.

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top