When I’m trying to create a role then I receive the error “role exists” and I have to deleted first and repeat the process.
How may I check if a role exists in my sql database in sql server 2008 r2?
Is there any sys.table to search for it?
use sys.database_principals view:
select * from sys.database_principals where name = #Role_Name and type = 'R'
it's also possible to use database_principal_id:
select database_principal_id(#Role_Name)
Related
I read in SQL Server documentation that I can specify column names in FROM clause of a simple SELECT query.
But when I try to run this query:
select * from my_db.dbo.test_table.test;
I get the following error:
Msg 7202, Level 11, State 2, Line 1 Could not find server 'my_db' in
sys.servers. Verify that the correct server name was specified. If
necessary, execute the stored procedure sp_addlinkedserver to add the
server to sys.servers.
I know this is a T-SQL page, and I'm trying to run .
Why does this happen?
I'm using SQL Server version 2017 via Microsoft SQL Server Management Studio version 2017 as well.
try rewriting from this
select * from my_db.dbo.test_table.test;
to this
select test_table.test,* from my_db.dbo.test_table;
the way it was written with that many Periods it assume you are trying to fully qualify the table so in what you had tried to the server is treating it as follows
my db = linked server (a server other than the Server you are working on)
dbo = Schema (which is correct)
test_table = Table (Also correct)
test = just plain erroror
the fields you want to show should directly follow the Keyword Select so if you only wanted 2 fields you could write
select test,test2 from my_db.dbo.test_table;
or simpler if you only have the one server
select test,test2 from dbo.test_table;
or if you only have the defailt Schema dbo (Database Base Owner)
select test,test2 from test_table;
I hope thaelps
You cannot specify columns in the FROM clause, but you can specify columns in the SELECT clause.
select test from my_db.dbo.test_table
Please run sp_addlinkedserver and sp_addlinkedsrvlogin SPs:
EXEC sp_addlinkedserver #server='MyLinkedServer'
EXEC sp_addlinkedsrvlogin 'MyLinkedServer', 'false', NULL, 'MyUserName', 'MyPassword'
After that, you can try to run: select * from my_db.dbo.test_table
Ref: https://blog.sqlauthority.com/2014/08/26/sql-server-fix-error-7202-could-not-find-server-in-sys-servers-verify-that-the-correct-server-name-was-specified/
Also, even though it is SQL Server 2014 related maybe helps, please see: Could not find server 'server name' in sys.servers. SQL Server 2014
I have two SQL Server database servers in two different server computers.
Server A - 192.168.1.100
Server B - 192.168.2.102
I need to execute a query from server A to retrieve data from a table in the Server B.
How to write the SQL select statement to perform this?
Are there any server configurations to allow these type of querying?
Add a linked server here are commands for 1 way of doing this. replace the user and password values with an appropriate SQL credential.
EXECUTE master.dbo.sp_addlinkedserver #server = N'192.168.2.102', #srvproduct=N'SQL Server'
EXECUTE master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'192.168.2.102',#useself=N'False',#rmtuser='ASQLLogin',#rmtpassword='Password'
then simply query like you would a normal table but append the linked server in front as commented above linked_server.db_name.schema_name.table_name like so:
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName]
You can even join it to your local server A if you want.
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName] b
INNER JOIN SomeTableOnServerA a
ON b.ID = a.ID
If I run the following SQL in SQL Server CE,
alter table audit add new_key nvarchar(100) null
Can that be changed to check whether the column exists before trying to add it in the same SQL statement? I am aware you can do that quite easily in SQL Server, but SQL Server CE?
If you are looking for something like you can do with SQL server: IF NOT EXISTS (..., unfortunately, this is not possible with SQL Server Compact (the T-SQL IF is not supported). See SQL Reference (SQL Server Compact).
Based on this thread, you can do something like this:
select * from Information_SCHEMA.columns
where Table_name='audit' and column_name='new_key'
(ie no result = does not exist).
Also note that batch queries are not supported in SQL Server Compact at all. So you can't check if the column exists and add the column if needed in the same SQL statement.
Or if you are looking for a snippet that you can use with VB.NET or C#, take a look at this SO question: How can I determine whether a column exists in a SQL Server CE table with C#?
The easiest way would be to query information_schema and see if the column is in there.
IF NOT EXISTS(
SELECT * FROM information_schema.columns
WHERE Table_Schema = 'dbo'
AND Table_Name = 'audit'
AND Column_Name = 'new_key'
) BEGIN
alter table audit add new_key nvarchar(100) null
END
I am looking for the best way to check that a database login exists in SQL Server 2005. I am currently using
IF suser_sid('loginname') IS NOT NULL
but suser_sid() returns a value in some cases where a login does not exist.
In SQL 2000 we use
SELECT * FROM [Master].[dbo].[sysxlogins] WHERE [name] ='loginname'
but that table does not exist in SQL 2005.
There is a similar question about checking the existence of Users, which is helpful, but I am looking for the existence of Logins.
For sql2005...
select * from master.sys.syslogins WHERE [name] ='loginname'
syslogins is not recommended by Microsoft. It's better to use sys.server_principals.
select * from sys.server_principals where name = 'login_name'
What is the SQL query to select all of the MSSQL Server's logins?
Thank you. More than one of you had the answer I was looking for:
SELECT * FROM syslogins
Is this what you're after?
select * from master.syslogins
On SQL Azure as of 2012;
logins:
--connecct to master
--logins
SELECT * from sys.sql_logins
--users
SELECT * from sys.sysusers
and users on a specific database:
--connect to database
SELECT * from sys.sysusers
Also note that 'users' on Azure SQL now (2022-11-17) have more 'login' type properties and creating a user on a Azure SQL database with a password is now possible, so it is less likely to require creating logins in 'master'.
EXEC sp_helplogins
You can also pass an "#LoginNamePattern" parameter to get information about a specific login:
EXEC sp_helplogins #LoginNamePattern='fred'
Starting with SQL 2008, you should use sys.server_principals instead of sys.syslogins, which has been deprecated.
#allain, #GateKiller your query selects users not logins
To select logins you can use this query:
SELECT name FROM master..sysxlogins WHERE sid IS NOT NULL
In MSSQL2005/2008 syslogins table is used insted of sysxlogins
Selecting from sysusers will get you information about users on the selected database, not logins on the server.
sp_helplogins will give you the logins along with the DBs and the rights on them.
Select * From Master..SysUsers Where IsSqlUser = 1
Have a look in the syslogins or sysusers tables in the master schema. Not sure if this still still around in more recent MSSQL versions though. In MSSQL 2005 there are views called sys.syslogins and sys.sysusers.