变量赋值的数据库错误。 [英] database error on variable assignment.

查看:62
本文介绍了变量赋值的数据库错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/**********************************************************************/
/* Install.SQL                                                        */
/* Creates a login and makes the user a member of db_owner            */
/*                                                                    */
/**********************************************************************/

-- Declare variables for database name, username and password
DECLARE @dbName sysname,
      @dbUser sysname,
      @dbPwd nvarchar(max);

-- Set variables for database name, username and password
SET @dbName = 'PlaceholderForDbName';
SET @dbUser = 'PlaceholderForDbUsername';
SET @dbPwd = 'PlaceholderForDbUserPassword';

DECLARE @cmd nvarchar(max)

-- Create login
IF( SUSER_SID(@dbUser) is null )
BEGIN
    print '-- Creating login '
    SET @cmd = N'CREATE LOGIN ' + quotename(@dbUser) + N' WITH PASSWORD ='''+ replace(@dbPwd, '''', '''''') + N''''
    EXEC(@cmd)
END

-- Create database user and map to login
-- and add user to the datareader, datawriter, ddladmin and securityadmin roles
--
SET @cmd = N'USE ' + quotename(@DBName) + N'; 
IF( NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = ''' + replace(@dbUser, '''', '''''') + N'''))
BEGIN
    print ''-- Creating user'';
    CREATE USER ' + quotename(@dbUser) + N' FOR LOGIN ' + quotename(@dbUser) + N';
    print ''-- Adding user'';
    EXEC sp_addrolemember ''db_owner'', ''' + replace(@dbUser, '''', '''''') + N''';
END'
EXEC(@cmd)
GO



Msg 911,Level 16,State 1,Line 1

数据库'PlaceholderForDbName'不存在。确保正确输入名称



帮助我解决此错误导致此错误的原因PlaceholderForDbName是一个varibale赋值然后数据库错误


Msg 911, Level 16, State 1, Line 1
Database 'PlaceholderForDbName' does not exist. Make sure that the name is entered correctly

help me with this error what caused this error PlaceholderForDbName is assignrd to a varibale then why database error

推荐答案

您好Varun,



您正在尝试连接到非现有数据库,这就是抛出错误的原因。
在@cmd变量中,您尝试使用非现有数据库。



如果需要,可以通过在sql server中键入以下命令来重新创建错误查询窗口

Hi Varun,

You are trying to connect to the non existing database that is why the error is thrown.
In @cmd variable you are trying to use non existing database.

You can recreate the error if you want by typing the below command in sql server query window
USE databasenotexist;



现在你可以在sql server studio中看到错误



尝试在这一行中对数据库名称进行硬编码这样的


now you can see the error in your sql server studio

Try hard coding the database name in this line like this

SET @dbName = 'your database name here';



并检查它。



问候,

RK


and check it.

Regards,
RK


可能是因为什么它实际上想要 您的数据库的名称



这只是一个猜测,基于实际字符串变量包含:PlaceholderForDbName - 这是(我认为)意味着你应该知道你应该把它放进去的线索...



另一个猜测:当你解决了这个问题后,你应该在该代码中查找占位符一词的任何其他实例......
Probably, because what it actually wants there is the name of your database

This is just a guess, based on the actual string the variable contains: "PlaceholderForDbName" - which is (I think) meant to be a clue to you as to what the heck you are supposed to put it in...

At another guess: when you have fixed that, you should probably look for any other instances of the word "Placeholder" in that code...


这篇关于变量赋值的数据库错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆