2

我的一个表中有一个 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 回答