Query to create a new table to a specific database in SQL? - sql-server

I have 3 databases sytemdatabases,smoketest and learnqueries .
Every time i write and execute create table query (create table tablename (colname datatype(size))), a new table is created in the systemdatabases.
I need it to be created to smoketest database.
I tried on this query (create table smoketest.newtablename(number int,name varchar(50));
It is showing an error (Msg 2760, Level 16, State 1, Line 1
The specified schema name "smoketest" either does not exist or you do not have permission to use it.) when i executed it.
It says 2 chances
Database does not exist - but the database exists
Does not have permission - what does it mean -how to set permission to create a
new table to a specific database
Pls help

The 4 part 'dot' notation in Sql Server for tables is
Server.Database.Schema.Object
So you would need to create the table with at least 3 parts if smoketest is not the current database on your connection, e.g. if you are on master, and assuming you want the new table in schema dbo:
create table smoketest.dbo.Tablename(ID INT)
Alternatively, switch to the smoketest database and create the table with 1 or 2 part naming:
use smoketest
GO
create table dbo.Tablename(ID INT)
GO

USE smoketest
GO
create table newtablename(number int,name varchar(50));
If you have a permission issue, check out this SQL Server 2008: how do I grant privileges to a username?
Hope this helps.

Related

CREATE TABLE returns error referring to sp_changeobjectowner and a non-existing schema

A little challenge for the MS Sql Server experts out there...
I have a database on a SQL Server 2008 R2 server.
I have created a schema named MPP and a handful of table for this schema.
The tables were originally created in the default dbo schema and then moved to the new schema using the sp_changeobjectowner procedure. The tables were however created and dropped a few times before I got it to work as I wanted.
To the problem... Whenever I now create a new table, regardless in the default dbo schema or I get one error message per table in the new schema referring to an error in the sp_changeobjectowner procedure:
CREATE TABLE dbo.test (d INT);
Msg 15001, Level 16, State 1, Procedure sp_changeobjectowner, Line 64
Object 'ASBJOR.TEST_TABELL' does not exist or is not a valid object for this operation.
There is a database user named ASBJOR, but no schema.
If I add a new table to the MPP schema the number of error messages will increase and one of the messages refers to the new table.
I've tried to create a new table in the default dbo schema and move it to the MPP schema using the ALTER SCHEMA MPP TRANSFER... command, but new table still appears in the error messages.
I've looked through the sys.objects, sys.tables, sys.schemas and sys.database_principals tables but I can't find anything wrong.
Any ideas what could cause this error message or where I can look for more hardcore details in the database?
You should never use sp_changeobjectowner - it's been deprecated since 2005. Always use ALTER SCHEMA ... TRANSFER.
As for the error, check for a DDL trigger in that database:
SELECT * FROM sys.triggers WHERE parent_class = 0;
Then check to see what those triggers are doing.

SQL Server equivalent of MySQL Dump to produce insert statements for all data in a table

I have an application that uses a SQL Server database with several instances of the database...test, prod, etc... I am making some application changes and one of the changes involves changing a column from a nvarchar(max) to a nvarchar(200) so that I can add a unique constraint on it. SQL Server tells me that this requires dropping the table and recreating it.
I want to put together a script that will do the table drop, recreate it with the new schema, and then reinsert the data that was there previously all in one go, if possible, just to keep things simple for use when I migrate this change to production.
There is probably a good SQL Server way to do this but I'm just not aware of it. If I was using Mysql I would mysqldump the table and its contents, and use that as my script for applying that change to production. I can't find any export functionality in SQL server that will give me a text file consisting of inserts for all data in a table.
Use SQL Server's Generate Scripts command
right click on the database; Tasks -> Generate Scripts
select your tables, click Next
click the Advanced button
find Types of data to script - choose Schema and Data.
you can then choose to save to file, or put in new query window.
results in INSERT statements for all table data selected in bullet 2.
No need to script
here are two ways
1 use alter table ....alter column.....
example..you have to do 1 column at a time
create table Test(SomeColumn nvarchar(max))
go
alter table Test alter column SomeColumn nvarchar(200)
go
2 dump into a new table while converting the column
select <columns except for the columns you want to change>,
convert(nvarchar(200),YourColumn) as YourColumn
into SomeNewTable
from OldTable
drop old table
rename this table to the same table as the old table
EXEC sp_rename 'SomeNewTable', 'OldTable';
Now add your index

SQL Server generate INSERTs to copy the records from one server to another

I am using
exec sp_generate_inserts 'TABLENAME'
to copy all the records of one table from our test server to live server.
Do we have to create the table first before using this command?????
I tried to give this command and it gives me an error - Invalid Object name.
I tried to create the table first and then to give this command. But in that case I have an identity column in the table and it gives an error..... Cannot insert identity value .
I know that the identity insert is set to off. Do I have to turn it on and try....How can turn it on.....And also could you please let me know if we have an alternative way of doing it.
Thanks
YES of course you have to create the table first before inserting data into it....
And if you want to insert specific values into an IDENTITY column, you can use
SET IDENTITY_INSERT (your table name) ON
-- do your INSERTs here
SET IDENTITY_INSERT (your table name) OFF

SQL Server parallels to Oracle Alter Table Set Column Unused?

Is there a parallel in Microsoft SQL Server (2005, preferably) for the Oracle functionality of setting a column to be unused? For example:
ALTER TABLE Person SET UNUSED Nickname;
Nothing turns up in searches, so my thought is this feature must be Oracle specific.
Don't think there's anything like that in SQL server.
You could create a 1:1 relation to a new table containing the hidden columns:
insert into NewTable
select (keycol, Nickname) from ExistingTable
alter table ExistingTable drop column Nickname
That way you still have the data, but the column is in a table nobody knows about.
Alternatively, you could use column level permissions:
DENY SELECT (Nickname) ON ExistingTable TO domain\user
DENY SELECT (Nickname) ON ExistingTable TO public
...
This will return an error when someone tries to read the column. The big disadvantage of this method is that select * will also fail.
There is no equivalent statement, but depending on your need you could probably write a trigger to roll back any changes if made.

sql script to create a new database

I have following script.
I don't know how to create new database in sql server 2005.
I run following script, and it create tables under model instead of seperate database. it look massy.
how can I create separate database.
I am copy some script here for your adive and course of action.
-----------------------------------------------------------
-- SQL Server 2000 Bible
-- Hungry Minds
-- Paul Nielsen
-- OBX Kites sample database - CREATE Database, Tables, and Procs
-- this script will drop an existing OBXKites database
-- and create a fresh new installation
-- related scripts:
-- OBXKites_Populate
-- T-SQL KEYWORDS go
-- DatabaseNames
-----------------------------------------------------------
-----------------------------------------------------------
-- Drop and Create Database
USE master
GO
IF EXISTS (SELECT * FROM SysDatabases WHERE NAME='OBXKites')
DROP DATABASE OBXKites
go
-- This creates 1 database that uses 2 filegroups
CREATE DATABASE OBXKites
ON PRIMARY
(NAME = 'OBXKites', FILENAME = 'D:\SQLData\OBXKites.mdf'),
FILEGROUP Static
(NAME = 'OBXKitesStatic', FILENAME = 'c:\SQLData\OBXKitesStatic.ndf')
LOG ON (NAME = 'OBXKitesLog', FILENAME = 'c:\SQLData\OBXKites.ldf')
go
-- set to Full Log
go
SET QUOTED_IDENTIFIER ON
go
USE OBXKites
go
-----------------------------------------------------------
-----------------------------------------------------------
-- Create Tables, in order from primary to secondary
CREATE TABLE dbo.OrderPriority (
OrderPriorityID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL DEFAULT (NEWID()) PRIMARY KEY NONCLUSTERED,
OrderPriorityName NVARCHAR (15) NOT NULL,
OrderPriorityCode NVARCHAR (15) NOT NULL,
Priority INT NOT NULL
)
ON [Static]
go
CREATE DATABASE OBXKites
ON PRIMARY
(NAME = 'OBXKites', FILENAME = 'D:\SQLData\OBXKites.mdf'),
FILEGROUP Static
(NAME = 'OBXKitesStatic', FILENAME = 'c:\SQLData\OBXKitesStatic.ndf')
LOG ON
(NAME = 'OBXKitesLog', FILENAME = 'c:\SQLData\OBXKites.ldf')
go
This code in your script should create a new database. If no error occurrs in the process.
Can you simplify it down? or do you have to do all of that checking?
If it can be simplified I beileve some code such as:
CREATE DATABASE OBXKites
USE OBXKites
CREATE TABLE OrderPriority(syntax)
If simple code like this doesn't work, you may have deeper issues with rights or files or something as suggested by queen3 just before me.
Afterthought: At my school we were having issues like this as well I believe... after a few classes we finally arived at the conclusion that we didn't have rights to add files to the program files directory on the lab computers and SQL wasn't reporting the error to us.
Are there any errors in output? The script itself does already do what you requested.
I'd suspect that you incorrectly specified filenames - some of them are on drive C:, and some on drive D:.
Your script in question will create the tables in the OBXKites database (see the USE OBXKites statement in your script)
Does the OBXKites database already exist in your server instance? If so, that's probably why it appears that it is creating the tables in the current database, since the script is effectively removing and recreating the OBXKites database. If you need a different name for the database, find all instances of "OBXKites" in your script above after the line that reads "This creates 1 database that uses 2 filegroups" and rename it to the new database name you desire.
The only other option could be that the script has execution errors creating the database, if your user account lacks the proper permissions to do so. However, the script continues to execute, creating the tables in the current database you are logged in to, which is why you are seeing the tables there.
Create DataBase In Ubuntu Terminal
my database name is database_name
you can use anything
create database database_name;
How to select this database to create a table inside it.
use database_name;

Resources