我可以用 abc-123 这样的连字符给 SQL Server 数据库名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20995394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:40:34  来源:igfitidea点击:

can I give SQL Server database name with hyphen like abc-123?

sqlsql-server

提问by Venu Gopal Reddy

I created a sql server database with name of abc-123, in that I created a table Emp, when I run like

我创建了一个名为 的 sql server 数据库abc-123,因为我创建了一个表 Emp,当我运行时

select * from abc-123.emp;
我正在得到结果。但是当我尝试向用户授予某些权限时,我无法做到这一点,getting syntax error near hyphen .在连字符附近出现语法错误。

will any one help me?

有人会帮助我吗?

回答by CRAFTY DBA

Make sure you are escaping the names with [] (T-SQL) or "" (ANSI SQL). You are using non-standard naming.

确保使用 [] (T-SQL) 或 "" (ANSI SQL) 对名称进行转义。您正在使用非标准命名。

-- Sample select
SELECT * FROM [abc-123].[dbo].[emp];
SELECT * FROM "abc-123"."dbo"."emp";

1 - Can you send me an example of the grant TSQL? If you are doing the action from SSMS, right click and script the code.

1 - 你能给我发一个授权 TSQL 的例子吗?如果您从 SSMS 执行操作,请右键单击并编写代码脚本。

2 - Here is the link to the GRANT TSQL command. I do not see any syntax like you are trying.

2 - 这是 GRANT TSQL 命令的链接。我没有看到像您正在尝试的任何语法。

http://technet.microsoft.com/en-us/library/ms188371.aspx

http://technet.microsoft.com/en-us/library/ms188371.aspx

TO 'drupal'@'localhost' IDENTIFIED BY 'Drup@l';

First, it should be [drupal@localhost]. Second, I never seen the IDENTIFIED BYclause. Where are you getting that information from?

首先,应该是[drupal@localhost]。其次,我从未见过该IDENTIFIED BY条款。你从哪里得到这些信息?

3 - Here is a quick TSQL script that creates a badly named database and user. If possible, change the name of the database and user.

3 - 这是一个快速的 TSQL 脚本,它创建了一个名称错误的数据库和用户。如果可能,请更改数据库和用户的名称。

Also, if you are granting permissions at the table level other than db_owner (very granular and a-lot of maintenance), then create an user defined database role. Add securables to the role and add your user to the role.

此外,如果您在 db_owner 以外的表级别授予权限(非常细化和大量维护),则创建用户定义的数据库角色。将安全对象添加到角色并将您的用户添加到角色。

http://technet.microsoft.com/en-us/library/ms187936.aspx

http://technet.microsoft.com/en-us/library/ms187936.aspx

Sample code.

示例代码。

-- Create new database
create database [abc-123]
go

-- Use new database
use [abc-123];
go

-- Create table from sample data
select 
       [BusinessEntityID]
      ,[PersonType]
      ,[NameStyle]
      ,[Title]
      ,[FirstName]
      ,[MiddleName]
      ,[LastName]
      ,[Suffix]
      ,[EmailPromotion]
      , cast([AdditionalContactInfo] as varchar(max)) 
        as [AdditionalContactInfoTxt]
      , cast([Demographics] as varchar(max)) 
        as [DemographicsTxt]
      ,[rowguid]
      ,[ModifiedDate]
into 
      [abc-123].[dbo].[emp]
from 
      AdventureWorks2012.Person.Person;

-- Create a login
CREATE LOGIN [drupal@localhost] WITH PASSWORD=N'Ja08n13$', DEFAULT_DATABASE=[abc-123]
GO

-- Create a user
CREATE USER [drupal@localhost] FOR LOGIN [drupal@localhost] WITH DEFAULT_SCHEMA=[dbo]
GO

-- Add to database owner role 
EXEC sp_addrolemember 'db_owner', [drupal@localhost]
GO

Output with user in db_owner group.

db_owner 组中的用户输出。

enter image description here

在此处输入图片说明

回答by Ajay

Use []round the database name:

使用[]四舍五入的数据库名称:

SELECT * FROM [abc-123].[dbo].emp;

OR

或者

SELECT * FROM [abc-123].dbo.emp;

回答by Radhamani Muthusamy

if you are using databasename with table name then suppose to specify the schemaname also.

如果您使用带有表名的 databasename 则假设也指定schema名称。

select * from [abc-123].dbo.emp

回答by kireeti9

Use Back Quote for the DB name

对数据库名称使用反引号

select * from `abc-123`.emp;

or, select the existing database with a USE statement and run the query.

或者,使用 USE 语句选择现有数据库并运行查询。

USE `abc-123`;
select * from emp;