Create database in stored procedure [duplicate] - sql-server

This question already has answers here:
SQL Server: use parameter in CREATE DATABASE
(2 answers)
Closed 6 years ago.
How can I create a database via a stored procedure?
CREATE PROCEDURE CreateDataBase
#strDBPath nvarchar(MAX),
#strFileName nvarchar(50),
#strLOGPath nvarchar(MAX),
#strLOGFileName nvarchar(50)
AS
BEGIN
CREATE DATABASE [APPLICATION]
CONTAINMENT = NONE
ON PRIMARY
( NAME = strFileName, FILENAME = strDBPath, SIZE = 5120KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = strFileName, FILENAME = strLOGPath , SIZE = 2048KB , FILEGROWTH = 10%)
END
GO
In SQL Server Management Studio everything seems alright. But in my Visual Studio database project the compiler complains about strDBPath and strLOGPath.
Error:
Incorrect Syntax near strDBPath

Because you try to use them as variables NOT USING THE PREFIX # in your statement. I am pretty sure, if you would READ the error message (quite telling you do not include it in your question here, you know) it would hint you in this direction.

Related

Restored SQL Server database taking too much space [duplicate]

This question already has answers here:
My log file is too large
(9 answers)
SQL Server Log File Is Huge
(4 answers)
SQL Server 2008 log file size is large and growing quickly
(4 answers)
Closed 7 months ago.
I generated .bak file from the production server. The size was around 2.2 GB. When I restored this file on my laptop the database _log.ldf file takes about 44 GB space. I then used Database right click -> Tasks -> Generate scripts menu. I chose both schema and data in advanced options and generated the script. These are the lines in beginning of script.
USE [master]
GO
/****** Object: Database [GRF_MDS] Script Date: 05-08-2022 14:18:42 ******/
CREATE DATABASE [GRF_MDS]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'GRF_MDS', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\GRF_MDS.mdf' , SIZE = 859584KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'GRF_MDS_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\GRF_MDS_log.ldf' , SIZE = 45686784KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT
I tried changing GRF_MDS_log.ldf size to 1 GB and ran the script but Datagrip hanged due to memory issues as the script was around 3 GB in size. I guess I can solve this problem by creating an application that reads the script line by line and executes. But is there any available option to reduce size? I have already tried Tasks -> Shrink -> database/ files menu.

Syntax Error with my SQL Create Table As [duplicate]

This question already has answers here:
How to create a table from select query result in SQL Server 2008 [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to create a new table in Microsoft SQL Server Management Studio based on two existing tables.
When I execute the query below, I get an error saying that there is an:
Incorrect syntax near the keyword 'SELECT'.
SQL code:
CREATE TABLE NEW_TABLE AS
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
I looked at various other problems, but could not find a solution to mine. Do you have any idea what could be wrong with the syntax?
Alternatively, you can use SELECT * INTO new_table statement just like this.
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C INTO NEW_TABLE
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
this statement will also create a new table as you required.

Can't use LONG data type

I'm trying to get some legacy SQL 2005 code to work on SQL 2012 Express. However, whenever I set the compatibility_level to 90, I error out when I try to use older data types. In theory the following code should work:
USE wsus_results
GO
ALTER DATABASE wsus_results
SET compatibility_level = 90
GO
CREATE TABLE ScTable (
TblName VARCHAR(255) NULL,
TblType VARCHAR(255) NULL,
FieldCnt INTEGER NULL,
RecordCnt LONG NULL,
Description LONGVARCHAR NULL,
TblId AUTOINCREMENT PRIMARY KEY)
GO
But, I get the following error:
Msg 2715, Level 16, State 6, Line 2 Column, parameter, or variable #4:
Cannot find data type LONG.
I'm sure there's something simple I'm missing, and I just need a nudge in the right direction. This isn't a permission issue and as far as I can tell, the SET compatibility_level = 90 executes fine with no errors. Still, I get an error when using LONG.
LONG is not a valid data type in any version of SQL Server. And changing compatibility level will not affect your ability to use old or new data types. This only affects the way certain language constructs are parsed.
Perhaps you meant DECIMAL or BIGINT.
And to pre-empt further questions: LONGVARCHAR and AUTOINCREMENT are not valid data types either (check the documentation instead of guessing). Where did you get this script, and who suggested it should work in SQL Server? I think you may have been pranked. Try this instead:
USE wsus_results;
GO
ALTER DATABASE wsus_results
SET compatibility_level = 110;
GO
CREATE TABLE dbo.ScTable -- schema prefix is important!
(
TblName VARCHAR(255),
TblType VARCHAR(255),
FieldCnt INT,
RecordCnt BIGINT,
Description VARCHAR(MAX),
TblId INT IDENTITY(1,1) NOT NULL PRIMARY KEY
);
GO
As an aside, is every other column in the table really nullable? Does your table name really need a suffix Table? What does Sc mean? Why not actually call the table what it represents (such as SocialCows or ScientificCholesterol) instead of obfuscating the name and adding a meaningless suffix just to incur more typing?

Transact-SQL using variable for table name [duplicate]

This question already has answers here:
A table name as a variable
(10 answers)
Closed 9 years ago.
I have try to use statement below in my SQL. Is it possible by any means to dynamically select table form database. (For example select table name from Comobox and then display data from selected table)
SET #var1 = 'test';
SELECT * From #var1
Dynamic SQL requires that you create a string consisting of the sql statement, and then you execute the string. Dynamic t-sql quotes in string

How to create an SQL Server 2008 database from script

I'm trying to do an Entity Framework walkthrough so I:
downloaded SQL script here: http://www.learnentityframework.com
in SQL Server Management Studio, I right-clicked Database, Create Database, named it
right-clicked on the new database, New Query
clicked on "Open File" and opened the script file: Create_ProgrammingEFDB1_SQLServer2008.sql
clicked "! Execute"
But the script (756K) has been running for 10 minutes now and still says "executing..."
My questions are:
Is this the standard way to read in an SQL script into SQL Server?
Is it supposed to take this long? This is how I would do it in MySQL/PHPMyAdmin it it might take a couple seconds, so I assume I'm not doing something right.
Here is the beginning of the script, I changed the file paths so they point to the right .mdf and .ldf files:
****/
--PART ONE CREATE THE DATABASE. Note the file paths in the first few commands.
--Change them for your own computer.--
USE [master]
GO
/****** Object: Database [ProgrammingEFDB1] Script Date: 01/28/2009 10:17:44 ******/
CREATE DATABASE [ProgrammingEFDB1] ON PRIMARY
( NAME = N'ProgrammingEFDB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1.mdf' , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ProgrammingEFDB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1_log.LDF' , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [ProgrammingEFDB1] SET COMPATIBILITY_LEVEL = 90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ProgrammingEFDB1].[dbo].[sp_fulltext_database] #action = 'disable'
end
...
ANSWER:
I had already created a database with the same name so it was trying to create a database that was already there which made it hang for some reason. I deleted that database, reran the script and it completed successfully in 3 seconds.
I don't know what does your script do exactly in the next 754K, but the lines you posted seem quite harmless.
Try adding the following to your script:
SET STATISTICS TIME ON
This will show queries execution times as they run, and it will help you to locate the problem more exactly.
But the script (756K)
Must be a lot more than just a CREATE DATABASE in the script, so very hard to say when the script is doing.
You can write progress reports from the script back to the client, or use SQL Profiler to see what commands are being executed.

Resources