August 17, 2005 at 1:41 pm
Hello All!
I am in need to ask some simple questions, so that i can begin to work in analyzer more often..
I did ask a similar question the other day and Phil answerd for me,, i would ask Phil this one but i am not sure if he will receive it.
So here i go..
While reading on how to use the templates in the Analyzer, i ran across the nifty little feature of replacement of parameters with the Replacement template Parameter tool. So i added my name in there, and low a behold after running the procedure it created a database with my name. (ok woooohooo).. here is the question,, it also created one table with 7 columns... where in the procedure is it saying to create columns and set the datatype??
Also it says IF EXISTS SELECT * FROM master.... is there a reference in the master database that is helping this create database template??
-- =============================================
-- Basic Create Database Template
-- =============================================
IF EXISTS (SELECT *
FROM master..sysdatabases
WHERE name = N'')
DROP DATABASE ErikLittle
GO
CREATE DATABASE
GO
Dam again!
August 17, 2005 at 1:57 pm
the exists is to check if the db exists. If it does exist, it is dropped then recreated. As for the table being created, check if you have a script in the window that might have been executed without you expecting that. Also check if that table is in model (will be copied to all new databases).
August 17, 2005 at 2:13 pm
In the --------> FROM master..sysdatabases <-------------
Is that just telling the querry to check the entire sqlserver?? For this data base..
Master in this case is not a database,, it is the entire server??
Confusion is coming in with me because i have a database with the name of Master.
ERIK...
Dam again!
August 17, 2005 at 2:20 pm
This is where the dbs' information is kept... and yes master is a database (system db, just like msdb and model).
August 17, 2005 at 2:39 pm
This Analyzer is much easier to use than i thought,, not sure what i was so scared of...
I have one more little one for today, (Unless i can sneak more than that in )
I understand that the template is creating a 1. database and the 2. a table,,,,,,question... how do i code this table template to be created right after the ----> = N'Demo_DB') database is created..??? I do not see anywhere in the table template the would tell the script to create the table in the newly created database ---> Demo_DB
1. Create database
2. create table and create it inside the new database --->Demo_DB
-- =============================================-- Basic Create Database Template-- =============================================IF EXISTS (SELECT * FROM master..sysdatabases WHERE name = N'Demo_DB') DROP DATABASE Demo_DBGOCREATE DATABASE Demo_DBGO-- =============================================-- Create table basic template-- =============================================IF EXISTS(SELECT name FROM sysobjects WHERE name = N'MyTable' AND type = 'U') DROP TABLE MyTableGOCREATE TABLE MyTable (ID int NULL, Description varchar(50) NOT NULL)GO-- =========================================================-- Backup database to disk file-- =========================================================backup database Demo_DB to disk = 'C:\mssql\backup\Demo_DB_ADHOC.bak' |
Dam again!
October 11, 2005 at 10:34 pm
If I understand your question correctly, you want to know how to tell the script to make the new table in the new database. Is this correct?
If so you'll want to add this at the start of the Create Table script:
Use Demo_DB
GO
Alternatively, you can be specific with the table name by using the database name, owner name and table name. So:
IF EXISTS(SELECT name FROM Demo_DB.dbo.sysobjects
WHERE name = N'MyTable' AND type = 'U') DROP TABLE Demo_DB.dbo.MyTable
GO
CREATE TABLE Demo_DB.dbo.MyTable (ID int NULL, Description varchar(50) NOT NULL)
GO
Hope this helps!
Martin
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply