SQL Server:变量删除表和架构

时间:2015-06-20 19:56:20

标签: sql-server variables

我正在尝试使用其他变量中的变量和其他变量来执行drop table。某些变量由查询定义,然后在其他查询的结果中使用。然而,当我解析时,我遇到了这个问题

  

命令已成功完成。

但是当我尝试执行时,我收到以下错误消息。

  

Msg 245,Level 16,State 1,Line 18
  转换varchar值时转换失败' S'数据类型int。

我无法弄清楚' S'

的实际价值。

第18行指的是

EXEC @defn_id_query

它所指的变量如下所列

SET @defn_id_query = (
'SELECT defn_id FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
AND defn_name = ''' + @schema + ''
)

以下是完整代码

DECLARE @defn_ver_id_query varchar
DECLARE @defn_ver_id int
DECLARE @defn_id_query varchar
DECLARE @defn_id int
DECLARE @schema varchar
DECLARE @deleteschema varchar
DECLARE @systemschema varchar
DECLARE @variableSET varchar
DECLARE @deletesreferences varchar
SET @schema = 'fpscdb001_ws_001'
SET @systemschema = '[fpscdb001_system]'
SET @defn_id_query = (
'SELECT defn_id FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
AND defn_name = ''' + @schema + ''
)
EXEC @defn_id_query
EXEC @defn_ver_id_query
SET @defn_id = (@defn_id_query)
SET @defn_ver_id = (@defn_ver_id_query)
SET @defn_ver_id_query = ('SELECT [defn_ver_id]
  FROM ' + @systemschema + '.[defn_ver]
  WHERE defn_id = @defn_id')
SET @deleteschema = (
'DROP TABLE ' + @schema + '.[id_generator]
DROP TABLE ' + @schema + '.[incident]
DROP TABLE ' + @schema + '.[incident_bal]
DROP TABLE ' + @schema + '.[incident_event_sum]
DROP TABLE ' + @schema + '.[incident_FH_HOURS]
DROP TABLE ' + @schema + '.[incident_FH_STAGE1]
DROP TABLE ' + @schema + '.[incident_FH_STAGE2]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3a]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3b]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3c]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3d]
DROP TABLE ' + @schema + '.[incident_FH_Stage4]
DROP TABLE ' + @schema + '.[incident_ST_STAGE1]
DROP TABLE ' + @schema + '.[incident_ST_STAGE2]DECLARE @defn_ver_id_query varchar
DECLARE @defn_ver_id int
DECLARE @defn_id_query varchar
DECLARE @defn_id int
DECLARE @schema varchar
DECLARE @deleteschema varchar
DECLARE @systemschema varchar
DECLARE @variableSET varchar
DECLARE @deletesreferences varchar
SET @schema = 'fpscdb001_ws_001'
SET @systemschema = '[fpscdb001_system]'
SET @defn_id_query = (
'SELECT defn_id FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
AND defn_name = ''' + @schema + ''
)
SET @defn_id = (@defn_id_query)
SET @defn_ver_id = (@defn_ver_id_query)
EXEC @defn_id_query
EXEC @defn_ver_id_query

SET @defn_ver_id_query = ('SELECT [defn_ver_id]
  FROM ' + @systemschema + '.[defn_ver]
  WHERE defn_id = @defn_id')
SET @deleteschema = (
'DROP TABLE ' + @schema + '.[id_generator]
DROP TABLE ' + @schema + '.[incident]
DROP TABLE ' + @schema + '.[incident_bal]
DROP TABLE ' + @schema + '.[incident_event_sum]
DROP TABLE ' + @schema + '.[incident_FH_HOURS]
DROP TABLE ' + @schema + '.[incident_FH_STAGE1]
DROP TABLE ' + @schema + '.[incident_FH_STAGE2]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3a]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3b]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3c]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3d]
DROP TABLE ' + @schema + '.[incident_FH_Stage4]
DROP TABLE ' + @schema + '.[incident_ST_STAGE1]
DROP TABLE ' + @schema + '.[incident_ST_STAGE2]
DROP TABLE ' + @schema + '.[incident_ST_STAGE3]
DROP TABLE ' + @schema + '.[incident_status]
DROP TABLE ' + @schema + '.[incident_transn]
DROP TABLE ' + @schema + '.[incident_transn_sum]
DROP TABLE ' + @schema + '.[item_field_journal]
DROP SCHEMA ' + @schema
)
SET @deletesreferences = ('
DELETE FROM ' + @systemschema + '.[defn_reln]
 WHERE from_defn_ver_id = @defn_ver_id
DELETE FROM  ' + @systemschema + '.[defn_ver]
WHERE defn_id = ' + @defn_id + '
DELETE FROM ' + @systemschema + '.[cal_wksp]
WHERE wksp_defn_id = ' + @defn_id + '
DELETE FROM ' + @systemschema + '.[bus_proc_defn]
WHERE defn_id = ' + @defn_id + '
DELETE FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
and defn_id = ' + @defn_id)
EXEC @deleteschema
EXEC @deletesreferences

DROP TABLE ' + @schema + '.[incident_ST_STAGE3]
DROP TABLE ' + @schema + '.[incident_status]
DROP TABLE ' + @schema + '.[incident_transn]
DROP TABLE ' + @schema + '.[incident_transn_sum]
DROP TABLE ' + @schema + '.[item_field_journal]
DROP SCHEMA ' + @schema
)
SET @deletesreferences = ('
DELETE FROM ' + @systemschema + '.[defn_reln]
 WHERE from_defn_ver_id = @defn_ver_id
DELETE FROM  ' + @systemschema + '.[defn_ver]
WHERE defn_id = ' + @defn_id + '
DELETE FROM ' + @systemschema + '.[cal_wksp]
WHERE wksp_defn_id = ' + @defn_id + '
DELETE FROM ' + @systemschema + '.[bus_proc_defn]
WHERE defn_id = ' + @defn_id + '
DELETE FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
and defn_id = ' + @defn_id)
EXEC @deleteschema
EXEC @deletesreferences

我对其他解决方案持开放态度,专业人士建议什么?

由于

编辑:

我猜测取消所有这些撇号让我很困惑......我试过以下

SET @defn_ver_id_query = ('SELECT [defn_ver_id]
FROM ' + @systemschema + '.[defn_ver]
WHERE defn_id = convert(nvarchar(max),' + @defn_id + '')

我也尝试过这种方式。

SET @defn_ver_id_query = ('SELECT [defn_ver_id]
FROM ' + @systemschema + '.[defn_ver]
WHERE defn_id = ' + convert(nvarchar(max),@defn_id) + '')

新的完整编辑

DECLARE @defn_ver_id_query varchar(max)
DECLARE @defn_ver_id int
DECLARE @defn_id_query varchar(max)
DECLARE @defn_id int
DECLARE @schema varchar(max)
DECLARE @deleteschema varchar(max)
DECLARE @systemschema varchar(max)
DECLARE @variableSET varchar(max)
DECLARE @deletesreferences varchar(max)
SET @schema = 'fpscdb001_ws_001'
SET @systemschema = '[fpscdb001_system]'
SET @defn_id_query = (
'SELECT defn_id FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
AND defn_name = ' + @schema + ''
)
SET @defn_id = (@defn_id_query)
SET @defn_ver_id = (@defn_ver_id_query)
EXEC (@defn_id_query)
EXEC (@defn_ver_id_query)

SET @defn_ver_id_query = ('SELECT [defn_ver_id]
  FROM ' + @systemschema + '.[defn_ver]
  WHERE defn_id = ' + convert(nvarchar(max),@defn_id) + '')
SET @deleteschema = (
'DROP TABLE ' + @schema + '.[id_generator]
DROP TABLE ' + @schema + '.[incident]
DROP TABLE ' + @schema + '.[incident_bal]
DROP TABLE ' + @schema + '.[incident_event_sum]
DROP TABLE ' + @schema + '.[incident_FH_HOURS]
DROP TABLE ' + @schema + '.[incident_FH_STAGE1]
DROP TABLE ' + @schema + '.[incident_FH_STAGE2]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3a]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3b]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3c]
DROP TABLE ' + @schema + '.[incident_FH_STAGE3d]
DROP TABLE ' + @schema + '.[incident_FH_Stage4]
DROP TABLE ' + @schema + '.[incident_ST_STAGE1]
DROP TABLE ' + @schema + '.[incident_ST_STAGE2]
DROP TABLE ' + @schema + '.[incident_ST_STAGE3]
DROP TABLE ' + @schema + '.[incident_status]
DROP TABLE ' + @schema + '.[incident_transn]
DROP TABLE ' + @schema + '.[incident_transn_sum]
DROP TABLE ' + @schema + '.[item_field_journal]
DROP SCHEMA ' + @schema
)
SET @deletesreferences = ('
DELETE FROM ' + @systemschema + '.[defn_reln]
 WHERE from_defn_ver_id = ' + convert(nvarchar(max),@defn_ver_id) + '
DELETE FROM  ' + @systemschema + '.[defn_ver]
WHERE defn_id = ' + convert(nvarchar(max),@defn_id) + '
DELETE FROM ' + @systemschema + '.[cal_wksp]
WHERE wksp_defn_id = ' + convert(nvarchar(max),@defn_id) + '
DELETE FROM ' + @systemschema + '.[bus_proc_defn]
WHERE defn_id = ' + convert(nvarchar(max),@defn_id) + ' 
DELETE FROM ' + @systemschema + '.[defn]
WHERE [defn_type_name] = ''CONTAINER''
AND defn_subtype_id = 1
and defn_id = ' + convert(nvarchar(max),@defn_id))
EXEC (@deleteschema)
EXEC (@deletesreferences)

1 个答案:

答案 0 :(得分:0)

我会建议:

  1. 如果您尝试将值连接到不是字符串的字符串,请转换它!示例:SET @string = 'Hello '+CONVERT(nvarchar(max),1);
  2. 如果您将值转换为NVARCHAR,则需要指定长度。示例:NVARCHAR(100)。在您的情况下,当您尝试将其连接到SQL查询时,您可以使用CONVERT(nvarchar(max),...)。之后它会回落到较低的长度。
  3. 您尝试在字符串中使用字符串。示例:SET @string = 'The String holds 'User'!'。这是错的。你需要在你的String中转义逗号,你可以通过doube引用来做到这一点。请参阅此示例:SET @string = 'The String holds ''User''!'
  4. 如果您尝试向字符串添加值,则需要在其间添加+:SET @string = 'Hello ' + 'User';
  5. 如果您尝试执行动态SQL(您尝试过),则需要评估SQL变量。您可以通过在字符串周围使用大括号来完成此操作示例:EXEC(@sqlString)。你尝试这样做:EXEC @sqlString。这种方式SQL Server查找名为@sqlString的过程,这是错误的。
  6. 希望这可以帮助您解决所有问题。