为什么此查询与错误的数据库名称匹配?

时间:2016-03-03 18:48:23

标签: sql-server tsql

为什么' TestDatabase2'还匹配' TestDatabase'?同样,' D2'也匹配' D'。

exec sp_executesql N'SELECT TOP 1
                    sdb.name AS DbName,
                    bus.backup_start_date AS LastBackUpTime,
                    bus.type AS Type
                FROM
                    sys.sysdatabases sdb 
                LEFT OUTER JOIN
                    msdb.dbo.backupset bus
                ON
                    bus.database_name = sdb.NAME
                    AND
                    bus.type = @backupType
                WHERE
                    sdb.name = @dbname
                ORDER BY
                    LastBackUpTime DESC',N'@dbName nvarchar(12),@backupType nvarchar(1)',@dbName=N'TestDatabase',@backupType=N'D'

1 个答案:

答案 0 :(得分:6)

数据库名称的参数是NVARCHAR(12)

猜猜TestDatabase有多少个字符;)

SELECT CAST('TestDatabase' as NVARCHAR(12)), CAST('TestDatabase12314' as NVARCHAR(12))

您应该将其设为NVARCHAR(128)SYSNAME,这是msdb.dbo.backupset.database_namesys.databases.name列的长度。