2

テーブルの1つにsmalldatetime列があり、デフォルト値は2012年1月1日です。

後で、実際のデフォルト値が正しい値に設定されていることを確認したいと思います。ただし、INFORMATION_SCHEMA.COLUMNSから値を読み戻すと、SQLにフォーマットが追加されています。次のコードは私の問題を示しています

DECLARE
  @Requireddefaultdate smalldatetime = 'Jan 1 2012',
  @Actualdefaultdate smalldatetime,
  @Actualdatestring nvarchar (128) ;

CREATE TABLE dbo.mytable 
       (thedate smalldatetime CONSTRAINT df_mytable_thedate DEFAULT 
        CAST('Jan 1 2012' AS smalldatetime)
 ) ;

INSERT INTO mytable DEFAULT VALUES;

SET @Actualdatestring = (SELECT column_default
                       FROM information_schema.columns
                       WHERE table_name = 'mytable') ;

PRINT @Actualdatestring; --result: (CONVERT([smalldatetime],'Jan 1 2012',0))

--Now I would like to convert @actualdatestring to smalldatetime  
--so I can compare it to @requireddefaultdate

SET @Actualdefaultdate = @Actualdatestring; -- gives error 'Conversion failed 
-- when converting character string to smalldatetime data type.'

SET @Actualdefaultdate = CAST((@Actualdatestring)AS smalldatetime); 
--gives same error as above

--Added below - this is the script I used for dynamic sql
declare @dynsql nvarchar(500), @paramdef nvarchar(500);
--  SET @dynsql = N'Set @param_actdate = CAST(@param_defaultstr AS smalldatetime)';
--Above line changed as below in response to comment/answers. 
--Now get single error: Incorrect syntax near '=' but seems to be closer to a correct solution.
SET @dynsql = ' ''Set '' + @param_actdate + '' = (Select  '' +  @param_defaultstr + '' )'' ';

SET @paramdef = N'@param_actdate = @Actualdefaultdate output, 
@param_defaultstr = @Actualdatestring';

EXECUTE sp_executesql @dynsql, @paramdef, 
@param_actdate = @Actualdefaultdate output, @param_defaultstr = @Actualdatestring;

--finally drop the table
DROP TABLE dbo.mytable;

また、パラメーターを指定してsp_executesqlを使用して変換を実行しようとしましたが、成功しませんでした(このスクリプトは上記に追加されています)。

@actualdatestringの日付部分だけを抽出するために文字列操作を行うことはできますが、もっとエレガントな方法が必要だと感じており、明らかな何かが欠けています

ご協力いただきありがとうございます

4

3 に答える 3

2
DECLARE 
  @Requireddefaultdate smalldatetime = 'Jan 1 2012', 
  @Actualdefaultdate smalldatetime, 
  @Actualdatestring nvarchar (128) ; 

CREATE TABLE dbo.mytable  
       (thedate smalldatetime CONSTRAINT df_mytable_thedate DEFAULT  
        CAST('Jan 1 2012' AS smalldatetime) 
 ) ; 

INSERT INTO mytable DEFAULT VALUES; 

SET @Actualdatestring = (SELECT column_default 
                   FROM information_schema.columns 
                   WHERE table_name = 'mytable') ; 

PRINT @Actualdatestring; --result: (CONVERT([smalldatetime],'Jan 1 2012',0)) 
exec('select ' + @actualdatestring)
DROP TABLE dbo.mytable; 

動的SQLを使用しているので少し厄介ですが、それでうまくいく可能性があります。

于 2012-06-06T15:10:54.050 に答える
0

私はまだ答えを完全に得ていませんが、これは私のはるかに改善された(そしてより短い)スクリプトです。結果ペインから文字列の内容をコピーすると正常に機能するのに、文字列自体を使用すると機能しない理由を理解するだけです。そこに着いたら、最終的な答えを投稿します。助けてくれてありがとう。

DECLARE 
  @Requireddefaultdate smalldatetime = 'Jan 1 2012', 
  @Actualdefaultdate datetime, 
  @Actualdatestring nvarchar (128),
  @dynql nvarchar(500), @paramdef nvarchar(500) ; 

CREATE TABLE dbo.mytable  
   (thedate smalldatetime CONSTRAINT df_mytable_thedate DEFAULT  
    CAST('Jan 1 2012' AS smalldatetime) 
 ) ; 
 INSERT INTO mytable DEFAULT VALUES; 

SET @Actualdatestring = (SELECT column_default 
                   FROM information_schema.columns 
                   WHERE table_name = 'mytable') ; 
--replace single quotes with double quotes for inclusion in dynamic sql string
SET @Actualdatestring = REPLACE (@Actualdatestring, '''', '''''') ;
print @Actualdatestring; --prints (CONVERT([smalldatetime],''Jan 1 2012'',0))
--copy the contents of @Actualdatestring from the results pane into the dynamic sql string
SET @dynsql  = 'Set @param =  (select (CONVERT([smalldatetime],''Jan 1 2012'',0)))'; -- works OK
--SET @dynsql  = 'Set @param =  (select @Actualdatestring)'; -- doesnt work,get conversion error
set @paramdef = N'@param smalldatetime output, @param2 nvarchar(128)'
EXECUTE sp_executesql @dynsql, @paramdef, @param = @Actualdefaultdate output, @param2 = @Actualdatestring;

if(@Actualdefaultdate <> @Requireddefaultdate)
begin
  print 'Error';
end
else
begin
  print 'OK'
end

--finally drop the table 
DROP TABLE dbo.mytable; 
于 2012-06-06T17:07:33.137 に答える
0

やっとやりたいことができました。SQLは文字列の内容に満足していると結論付けましたが、文字列変数自体を使用すると、SQLはカバーションエラーを宣言しました。したがって、私のアプローチは、sp_executesql を呼び出す前に文字列の内容を埋め込むことでした。

DECLARE 
  @Requireddefaultdate smalldatetime = 'Jan 1 2012', 
  @Actualdefaultdate datetime, 
  @Actualdatestring nvarchar (128),
  @dynsql nvarchar(500), @paramdef nvarchar(500) ; 

CREATE TABLE dbo.mytable  
   (thedate smalldatetime CONSTRAINT df_mytable_thedate DEFAULT  
    CAST('Jan 1 2012' AS smalldatetime) 
 ) ; 
 INSERT INTO mytable DEFAULT VALUES; 

SET @Actualdatestring = (SELECT column_default 
                   FROM information_schema.columns 
                   WHERE table_name = 'mytable') ; 
--embed any string variables that need to be concatenated before calling sp_executesql
SET @dynsql  = 'Set @param =  (select ' + @Actualdatestring + ')'; 
set @paramdef = N'@param smalldatetime output'
EXECUTE sp_executesql @dynsql, @paramdef, @param = @Actualdefaultdate output;

if(@Actualdefaultdate <> @Requireddefaultdate)
begin
  print 'Error';
end
else
begin
  print 'OK'
end

--finally drop the table 
DROP TABLE dbo.mytable; 
于 2012-06-06T17:33:17.470 に答える