0

我在简化此查询时遇到问题。
我需要将 12 设为变量,因此我只需设置一次,并且%BrightSide08.

因为我需要用不同的名称和长度重复这个。

Declare @len int, @name varchar

SET @len = 12
SET @name = '%BrightSide08'

UPDATE CHAR_DATA0
SET CHAR_DATA = (SELECT CHAR_DATA FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) LIKE '%BrightSide08')
WHERE CHAR_KEY = (
                SELECT top (1) CHAR_KEY
                FROM CHAR_DATA0
                 WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) LIKE '%BrightSide08')
                 )

UPDATE CHAR_INFOR
SET CHARID0 = (SELECT CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) FROM xtreme.dbo.CHAR_DATA0 
               WHERE CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) LIKE '%BrightSide08')
WHERE USER_KEY = (
                SELECT top (1) USER_KEY
                FROM CHAR_DATA0
                 WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) LIKE '%BrightSide08')
                 )


SELECT *
FROM CHAR_DATA0
WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) LIKE '%BrightSide08')

SELECT *
  FROM [game].[dbo].[CHAR_INFOR]
  WHERE USER_KEY = (
                SELECT top (1) USER_KEY
                FROM game.dbo.CHAR_DATA0
                 WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(12), SUBSTRING(CHAR_DATA, 9, 16)) LIKE '%BrightSide08')
                 )

我错过了什么吗?请纠正我。谢谢你。

4

1 回答 1

0

指定数据类型时不能提供变量。尝试制作一个动态 sql 字符串:

Declare @len int, @name varchar

SET @len = 12
SET @name = '%BrightSide08'

DECLARE @strSql NVARCHAR(MAX) = 
N'UPDATE CHAR_DATA0
SET CHAR_DATA = (SELECT CHAR_DATA FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) LIKE ''' + @name + ''')
WHERE CHAR_KEY = (
                SELECT top (1) CHAR_KEY
                FROM CHAR_DATA0
                 WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) LIKE ''' + @name + ''')
                 )

UPDATE CHAR_INFOR
SET CHARID0 = (SELECT CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) FROM xtreme.dbo.CHAR_DATA0 
               WHERE CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) LIKE ''' + @name + ''')
WHERE USER_KEY = (
                SELECT top (1) USER_KEY
                FROM CHAR_DATA0
                 WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) LIKE ''' + @name + ''')
                 )


SELECT *
FROM CHAR_DATA0
WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) LIKE ''' + @name + ''')

SELECT *
  FROM [game].[dbo].[CHAR_INFOR]
  WHERE USER_KEY = (
                SELECT top (1) USER_KEY
                FROM game.dbo.CHAR_DATA0
                 WHERE USER_KEY = (SELECT USER_KEY FROM xtreme.dbo.CHAR_DATA0
                 WHERE CONVERT(VARCHAR(' + @len + '), SUBSTRING(CHAR_DATA, 9, 16)) LIKE ''' + @name + ''')
                 )'

EXEC sp_executesql @strSql

不幸的是,动态 SQL 并不能完全“简化”您的查询。

于 2013-09-06T03:15:22.563 回答