Database creation in master - sql

Now here is the query am trying to run to execute a script that will build up my database according to the script:
USE master
GO
if exists (select * from sysdatabases where name='Northwind')
drop database Northwind
go
DECLARE #device_directory NVARCHAR(520)
SELECT #device_directory = SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1
EXECUTE (N'CREATE DATABASE Northwind
ON PRIMARY (NAME = N''Northwind'', FILENAME = N''' + #device_directory + N'northwnd.mdf'')
LOG ON (NAME = N''Northwind_log'', FILENAME = N''' + #device_directory + N'northwnd.ldf'')')
go
exec sp_dboption 'Northwind','trunc. log on chkpt.','true'
exec sp_dboption 'Northwind','select into/bulkcopy','true'
GO
Now here is the error am getting...
Msg 262, Level 14, State 1, Line 1
CREATE DATABASE permission denied in database 'master'.
Msg 15010, Level 16, State 1, Procedure sp_dboption, Line 64
The database 'Northwind' does not exist. Supply a valid database name. To see available databases, use sys.databases.
Msg 15010, Level 16, State 1, Procedure sp_dboption, Line 64
The database 'Northwind' does not exist. Supply a valid database name. To see available databases, use sys.databases.
Kindly help! The database should be made now in master but its not!

Your should have CREATE DATABASE permission to create new datebase. If you are not familiar with this topic, try to login as "sa" user (if you know password), or ask administrator to add your login to "sysadmin" role at SQL Server.

Related

Get list of SQL server databases which files have been deleted

My goal is getting list of SQL server databases which files have been deleted.
In other words. I attach a db from mount, then close the mount so actually I have the attached db without files.
At first it seemed to be easy. Just pretty easy select:
SELECT
'DB_NAME' = db.name,
'FILE_NAME' = mf.name,
'FILE_TYPE' = mf.type_desc,
'FILE_PATH' = mf.physical_name
FROM
sys.databases db
INNER JOIN sys.master_files mf
ON db.database_id = mf.database_id
WHERE
--and specific condition here
But it turned out differently. Sql server has almost the same information about a regular database and a database which doesn't have files. So I had to try something else.
Further I tried to use state of database. And it was quite strange.
Unfortunately the following query gives me wrong(or not actual information):
SELECT state
FROM sys.databases
WHERE name = N'TestDB'
state
-----
0
And 0 means ONLINE according to this link
But actually the database has RECOVERY_PENDING state. It looks like that sql server information about my TestDB us out of date and should be refreshed. But I have no idea how to achieve this. But after executing any of following query this info(db state) is being refreshed:
EXEC sp_helpdb N'TestDB'
ALTER DATABASE N'TestDB' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
USE N'TestDB'
--etc
--all requests are terminated with the same error
Msg 5120, Level 16, State 101, Line 10
Unable to open the physical file "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB.mdf". Operating system error 3: "3(The system cannot find the path specified.)".
File activation failure. The physical file name "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB_log.ldf" may be incorrect.
File activation failure. The physical file name "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB_log-2.ldf" may be incorrect.
Msg 5181, Level 16, State 5, Line 10
Could not restart database "TestDB". Reverting to the previous status.
Msg 5069, Level 16, State 1, Line 10
ALTER DATABASE statement failed.
So do you have any idea ?
And also I've asked the question looks like this here differently.
Finally, I've found what i actually need.
I can chech whether the specific file exists or not by sql server:
CREATE FUNCTION dbo.fn_FileExists(#path varchar(512))
RETURNS BIT
AS
BEGIN
DECLARE #result INT
EXEC master.dbo.xp_fileexist #path, #result OUTPUT
RETURN cast(#result as bit)
END;
GO
So i just need to execute the function above for each file which i can get by executing for example following query:
SELECT
DISTINCT 'FILE_PATH' = physical_name
FROM sys.master_files

Table structure using sp_help

When I tried this query:
sp_help '[OG_System].[dbo].[tbl_act]'
This message appears:
Msg 15250, Level 16, State 1, Procedure sp_help, Line 48
The database name component of the object qualifier must be the name of the current database.
Because your target database is different than current database.
Use;
EXEC [ServerName].[DatabaseName].dbo.sp_help 'tbl_act'
thanks goGud, I know the correct usage of sp_columns.
EXEC [ServerName].[DatabaseName].dbo.sp_columns 'tbl_act'

The SELECT permission was denied on the object 'sysaltfiles', database 'mssqlsystemresource', schema 'sys'

Here is the query that iam trying to run on my SQL Management Studio 2008 R2:
USE master
GO
if exists (select * from sysdatabases where name='Northwind')
drop database Northwind
go
DECLARE #device_directory NVARCHAR(520)
SELECT #device_directory = SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1
EXECUTE (N'CREATE DATABASE Northwind
ON PRIMARY (NAME = N''Northwind'', FILENAME = N''' + #device_directory + N'northwnd.mdf'')
LOG ON (NAME = N''Northwind_log'', FILENAME = N''' + #device_directory + N'northwnd.ldf'')')
go
exec sp_dboption 'Northwind','trunc. log on chkpt.','true'
exec sp_dboption 'Northwind','select into/bulkcopy','true'
GO
And here is complete error am getting on my screen:
The SELECT permission was denied on the object 'sysaltfiles', database 'mssqlsystemresource', schema 'sys'.
Msg 262, Level 14, State 1, Line 1
CREATE DATABASE permission denied in database 'master'.
Msg 15010, Level 16, State 1, Procedure sp_dboption, Line 64
The database 'Northwind' does not exist. Supply a valid database name. To see available databases, use sys.databases.
Msg 15010, Level 16, State 1, Procedure sp_dboption, Line 64
The database 'Northwind' does not exist. Supply a valid database name. To see available databases, use sys.databases.
Looks like permissions are messed up. But i can't figure out what to do. Please a well explained answer with step by step guidance would be very appreciated. Thank You!
enter the all domain name to the connection string to the data source ...data source=dbname.doma.main.local

How - create and use database directly after creation in SQL Server?

I created a sql script to check if a database already exist, if it already exists it deletes and re-creates.
After I would like to connect it directly after its creation for creating tables ..
Here is my code but it does not work.
He announces an error message
Msg 911, Level 16, State 1, Line 10
Database 'Arms2' does not exist. Make sure that the name is entered correctly.
My script
IF EXISTS (select * from sys.databases where name = 'Arms2')
BEGIN
DROP DATABASE Arms2
PRINT 'DROP DATABASE Arms2'
END
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
USE Arms2
CREATE TABLE .....
Put a GO statement after the CREATE...
...
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
GO
USE Arms2

SQL Server : add CSV programmable

I have the following code to insert a CSV file into a temp table:
USE [websitehere.com].[dbo]
GO
CREATE TABLE [websitehere.com].[dbo].[tmpTable]
(ID INT,
Caller_Number VARCHAR(100),
Caller_Name VARCHAR(100),
GroupBy VARCHAR(100),
Campaign_Name VARCHAR(100),
DateAndTime VARCHAR(100),
Duration VARCHAR(100),
Call_Status VARCHAR(100))
GO;
BULK
INSERT tmpTable
FROM 'C:\Users\namehere\Documents\CallLog.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
But...
It does not seem to add the tmpTable. It gives me an error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.
It won't add the insert command saying that
Msg 4834, Level 16, State 4, Line 8
You do not have permission to use the bulk load statement.
Any help would be great to take care of these 2 errors!
Update
The line
USE [websitehere.com].[dbo]
should say
USE [websitehere.com]
The second error should be fixed by assigning the propper permissions or role memeberships for your user. You need INSERT and ADMINISTER BULK OPERATIONS permissions. Maybe more, depending on what other options your bulk insert statement might have.
You might want to try this with some test user account:
USE master;
GO
CREATE LOGIN bulkuser WITH PASSWORD = 'P#ssw0rd';
GRANT ADMINISTER BULK OPERATIONS to bulkuser;
USE [websitehere.com]
GO
CREATE USER bulkuser FOR LOGIN bulkuser;
EXEC sp_addrolemember 'db_datareader', 'bulkuser'
GRANT INSERT ON [dbo].[tmpTable] TO bulkuser;
Then login as "bulkuser" and try running your BULK INSERT statement.
Ah found where i needed to grant access to be able to use it!
Right-click on your database table >
properties >
select Permissions from the left panel >
click the blue link "View server permissions" >
check "Grant" for "Administer bulk operations" >
Ok > Ok > Done!