Skip to content

Instantly share code, notes, and snippets.

@kiyokura
Created December 14, 2015 05:08
Show Gist options
  • Save kiyokura/2067949821ae6020d023 to your computer and use it in GitHub Desktop.
Save kiyokura/2067949821ae6020d023 to your computer and use it in GitHub Desktop.
BEGIN
DECLARE @A AS VARCHAR(MAX) = NULL
DECLARE @B AS VARCHAR(MAX)
-- パターン1:先に結合した文字列を作ってからEXECUTEに渡す
SET @B = 'SELECT ''' + @A + ''' AS COL1'
EXECUTE(@B)
-- パターン2:EXECUTEの引数内で+演算子で結合?
EXECUTE('SELECT ''' + @A + ''' AS COL1')
-- =========================================
-- 結果
-- =========================================
-- パターン1: @BがNULLのため何も結果が得られない
-- パターン2: @Aを空文字列として組み立てられた「SELECT '' AS COL1」が実行される
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment