我的一个表中有一个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的日期部分,但我觉得必须有一个更优雅的方法,我错过了一些明显的东西
感谢您的帮助
答案 0 :(得分: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,但这可能适合你。
答案 1 :(得分: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;
答案 2 :(得分: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;