2

我无法让某个查询在 SSMS 之外工作。该查询收集所有用户定义的数据类型并创建 IF EXISTS 语句和 CREATE TYPE 命令,以便轻松传输所有用户定义的数据类型。这样做的查询如下(不是我写的,而是在https://stackoverflow.com/a/12761027/865893找到的)

SET NOCOUNT ON

SELECT 'USE ' + QUOTENAME(DB_NAME(), '[') + '
GO';

SELECT '
IF  EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N''' + st.[name] + ''' AND ss.name = N''' + ss.[name] + ''')
    DROP TYPE ' + QUOTENAME(ss.name, '[') + '.' + QUOTENAME(st.name, '[') + '
GO

CREATE TYPE ' + QUOTENAME(ss.name, '[') + '.' + QUOTENAME(st.name, '[') + ' FROM ' + 
QUOTENAME(bs.[name], '[') + 
    CASE bs.[name]
        WHEN 'char' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length) + ')' END)
        WHEN 'nchar' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length/2) + ')' END)
        WHEN 'varchar' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length) + ')' END)
        WHEN 'nvarchar' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length/2) + ')' END)
        WHEN 'numeric' THEN (CASE ISNULL(st.[precision], 0) WHEN 0 THEN '' ELSE '(' + convert(varchar(10), st.[precision]) + ', ' + convert(varchar(10), st.[scale]) + ')' END)
        WHEN 'decimal' THEN (CASE ISNULL(st.[precision], 0) WHEN 0 THEN '' ELSE '(' + convert(varchar(10), st.[precision]) + ', ' + convert(varchar(10), st.[scale]) + ')' END)
        WHEN 'varbinary' THEN (CASE st.max_length WHEN -1 THEN '(max)' ELSE '(' + convert(varchar(10), st.max_length) + ')' END)
        ELSE ''
    END + 
'
GO
'
FROM sys.types st 
    INNER JOIN sys.schemas ss ON st.[schema_id] = ss.[schema_id]
    INNER JOIN sys.types bs ON bs.[user_type_id] = st.[system_type_id]
WHERE st.[is_user_defined] = 1 -- exclude system types
ORDER BY st.[name], ss.[name]

这就像 SSMS 中的魅力一样,但我希望不必每次都进入 SSMS 并自己将其导出到文件中。当我通过批处理文件中的 osql 命令运行查询时,如下所示。

@echo off
cls

set /p SName=Server Name :
set /p UName=User Name :
set /p Pwd=Password :
set /p DbName=Database Name :

set /p choice=ARE YOU SURE TO EXECUTE SCRIPTS in %DbName% (y/n) ?

if '%choice%'=='y' goto begin
goto end

:begin
if exist _Deploy.txt del _Deploy.txt

@echo on

DEL addUserDefinedTypes.txt
osql -n -S %SName% -d master -U %UName% -i "getTypesSelectQuery.sql" -o addUserDefinedTypes.txt -P %Pwd%

@notepad addUserDefinedTypes.txt

:end

然而,这给了我以下输出:

Msg 105, Level 15, State 1, Server US1524NB, Line 8
Unclosed quotation mark after the character string '
'.
Msg 102, Level 15, State 1, Server US1524NB, Line 8
Incorrect syntax near '
'.
Msg 102, Level 15, State 1, Server US1524NB, Line 2
Incorrect syntax near ' + QUOTENAME(ss.name, '.
Msg 105, Level 15, State 1, Server US1524NB, Line 14
Unclosed quotation mark after the character string '
'.
Msg 105, Level 15, State 1, Server US1524NB, Line 1
Unclosed quotation mark after the character string '
FROM sys.types st 
INNER JOIN sys.schemas ss ON st.[schema_id] = ss.[schema_id]
INNER JOIN sys.types bs ON bs.[user_type_id] = st.[system_type_id]
WHERE st.[is_user_defined] = 1 -- exclude system types
ORDER BY st.[name], ss.[name]
'.

任何线索都会很棒。我环顾谷歌一段时间,不知道要改变什么/如何解决这个问题。像它所说的那样删除或关闭引号会破坏事情。谢谢。

4

1 回答 1

1

您使用的是什么版本的 SQL Server?

从 SQL Server 2005 开始,从命令行执行 SQL 脚本的首选方法是SQLCMD代替osql.

有关可用 cmd 线路开关的更多详细信息,请参阅MSDN SQL Server 联机丛书主题。SQLCMD

于 2013-01-10T21:29:57.693 回答