2012-06-06 30 views
2

我在我的表之一,2012年轉換INFORMATION_SCHEMA.COLUMNS COLUMN_DEFAULT到基礎數據類型

後來我想檢查的實際默認值已設置爲正確的1月1日的默認值的SMALLDATETIME列值。但是,當我讀回來自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

'sp_executesql'應該可以工作,你有什麼問題?您需要將字符串文字中的單引號加一。你爲什麼要檢查這個? –

+0

謝謝。我已經接受了你的評論和Triple的回答 - 特別是關於轉義單引號並在上面編輯了我的腳本。它還沒有工作,但我認爲它更接近。原因是我的數據庫表是從一組表中自動創建的,這些表構成了數據庫需求。這種檢查默認值的算法是我的單元測試套件的一部分。謝謝你的幫助。 –

+0

其實我可能在那裏錯誤地發送了你。 'DECLARE @DynSQL nvarchar(max)= N'SELECT @Result ='+ @Actualdatestring; DECLARE @Result smalldatetime; EXEC sp_executesql @ DynSQL,N'@ Result smalldatetime OUTPUT',@ Result OUTPUT; SELECT @ Result' –

回答

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,但這可以爲你工作。

+0

感謝你自己和馬丁。根據這些評論我已經稍微改變了我的劇本,雖然我還沒有得到它的工作,但我認爲這更接近正確的解決方案。使用select似乎是刪除格式的關鍵。我沒有使用你的exec方法,因爲我需要從選擇中「抓取」輸出,所以我可以將它與指定的值進行比較。這是我使用sp_executesql的原因。謝謝 –

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; 
0

我終於得到了一些工作。我總結說sql對字符串的內容感到滿意,但是當我使用字符串變量本身時,sql聲明瞭一個coversion錯誤。所以我的方法是在調用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;