SQL Server 2008 - permission denied in database 'master' - sql

I am new to SQL Server and I wanted to create my first table there.
create table Employee
(
ID smallint not null
)
I use SQL Server 2008 R2 and Windows Authentication.
when I execute , it says :
CREATE TABLE permission denied in database 'master'.
Thanks!

Seems you're trying to create the table in master database where you may not have permission to create table. However, to create your target database please follow below steps:
a. At your SQLQuery editor choose your target database (Available Database drop down list) and execute your sql query.
Or
b. Try with below statement:
USE YourTargetDatabaseName
GO
CREATE TABLE Employee ( ID SMALLINT NOT NULL)
GO

I don't think you want to create a table in the master database.
Did you create a new database first? If so, use this:
USE [MyNewDatabaseName]
GO
create table Employee ( ID smallint not null )
GO

Related

Best way to transfer data from source table in one db to destination table in another db daily

What would be the best way to transfer certain number of records daily from source to destination and then remove from source?
DB - SQL server on cloud.
As the databases are in the same server, you can create a job that transfers the data do the other database.
Because the databases are in the same server you can easily access them, just by adding the database before the table in the query, look the test that i did:
CREATE DATABASE [_Source]
CREATE DATABASE [_Destination]
CREATE TABLE [_Source].dbo.FromTable
(
some_data varchar(10)
)
CREATE TABLE [_Destination].dbo.ToTable
(
some_data varchar(10)
)
INSERT INTO [_Source].dbo.FromTable VALUES ('PAULO')
--THE JOB WOULD BE SOMETHING LIKE THIS:
-- INSERT INTO DESTINATION GETTING THE DATA FROM THE SOURCE
INSERT INTO [_Destination].dbo.ToTable
SELECT some_data
FROM [_Source].dbo.FromTable
-- DELETE FROM SOURCE
DELETE [_Source].dbo.FromTable

Microsoft SQL Server 2005: Create alias for database

How can I create alias name to my database in SQL Server 2005?
For example: DB1 and its alias DB2, it's same DB, but with two names.
Or can I do replication, mirroring, syncing or anything other inside server from one DB to another?
You can do replication from one database to another database on the same machine. You can also copy data directly without having to create an alias. For instance if you had a table named Users in DB2 and a Users table in DB1 and they are the same schema you could easily just do
INSERT INTO DB1..Users
select * from DB2..Users
Now, a synonym would allow you to use a table from DB2 as if it was a table in DB1 so for instance if you have a table named Products in DB2 you could do
use DB1
GO
CREATE SYNONYM [dbo].[Products] FOR [DB2].[dbo].[Products]
GO
-- Now the following would give you the same result
select * from DB2..Products
select * from Products
For more information on synonyms see here

Using Synonym in SQL Server 2008 R2 (Create and detect existence of a table)

Is there any way to CREATE a table on SQL Server 2008 R2 by referencing the table name with a synonym? Also how can I detect if any object (the base object) is existing in the database by its synonym?
I've recently discovered that you can create a synonym for an object that does not even exist in the database. It is kind of weird. But it might just be VERY useful for an implementation I'm trying to do.
So if I need to create a table named dbo.Customer, I want to do the following-
/* The table dbo.Customer has not been created in the database yet */
CREATE SYNONYM Customer_Synonym FOR dbo.Customer
/* I know the lines below will give the wrong result (the object_id detection part)
* and an error (the create table part), cause I've tried it.
* I want to know if there is any simple work around for this
*/
IF OBJECT_ID(N'Customer_Synonym') IS NOT NULL
CREATE TABLE Customer_Synonym
(
cust_id INT NOT NULL
, cust_name VARCHAR(100) NOT NULL
...
...
);
Any help or suggestion will be very much appreciated. Thanks.

SQL Server - Display the username in a column

We have a SQL Server 2012 database that 4 developers use, lets call them, user1, user2, user3, user4.
I want to create a column in one of the tables lets call the column User_Input, this column needs to show the username of the developer who insert any data, is this possible? For example if user2 inserted a new record, the column User_Input should display user2.
Please let me know if SQL Server does not support that, and if there is any other solution cause I searched the ## functions for SQL Server and non of them seems to get the username.
SYSTEM_USER function will return the login name of the user.
You can test it out with this statement:
SELECT SYSTEM_USER
The MSDN documentation for SYSTEM_USER states:
You can use the SYSTEM_USER function with DEFAULT constraints in the
CREATE TABLE and ALTER TABLE statements. You can also use it as any
standard function.
Here is a quick example of how to create a table with DEFAULT constraint that inserts the SYSTEM_USER into the User_Input column.
CREATE TABLE MyTable
(
ID int,
Value varchar(30),
User_Input varchar(200) DEFAULT SYSTEM_USER
)
I believe this is what you're looking for.
SELECT SYSTEM_USER
You can add this to inserts/updates as needed.
CURRENT_USER provides the schema, not the login associated with the transaction.

How to properly trigger an insert to a linked sql server?

I have the same table in two different sql servers (one is SqlServer 2000 and the other 2008).
I'm using sql server management studio.
I want that each time an insert is made on a table in the SqlServer 2000 table (Table_1) a trigger occurs and the record would also be inserted to the same table in the SqlServer 2008 table (also Table_1).
the sql server 2008 is defined as a linked server, and it is possible to run queries and perform inserts on the 2008 db from the 2000 db connection using sql server management studio.
the trigger is defined as follows :
ALTER TRIGGER [dbo].[trgrTable]
ON [dbo].[Table_1]
AFTER INSERT
AS
BEGIN
INSERT INTO [TLVSQL].[AVI_DEV].[dbo].[Table_1](ID, Value)
SELECT INSERTED.ID AS ID, INSERTED.Value AS Value
FROM INSERTED
END
the [TLVSQL].[AVI_DEV] is the 2008 db name.
But every time i perform an insert on the 2000 table i get a message that the insert failed to commit due to "sqloledb was unable to begin a distributed transaction linked server ...".
The security delegation of the linked server is well defined, i explicitly set the security credentials to a user/password of a db_owner.
What am i doing wrong ? Is there another way of doing what i ask ?
thank you.
Perform inserts from a trigger into a table on linked server - bad decision.
This will great affects on insert performance in the source table ([dbo].[Table_1])
and also there is a distributed transaction, and configuring servers
for support distributed transactions - nightmare.
One possible solution is:
On the source server you can create a synchronization queue table. For example:
CREATE TABLE dbo.SyncQueue
(
QueueId INT IDENTITY(1,1),
KeyForSync INT, -- Primary key value of record in dbo.SourceTable
SyncStatus INT -- statuses can be: 0 - New, 1 - Synchronized, 2 - Error
)
suppose you source table is
CREATE TABLE dbo.SourceTable
(
Key INT, -- primary key of the table
Data varchar(xxx)
)
Triger on dbo.SourceTable can quickly insert into dbo.SyncQueue record Key that you need synchronize
Some periodically performed stored procedure can then insert records from the queue in
table on the linked server.