I wish to create a table T1. And when I execute the query that table should be include in HumanResources schema which already exists in the database.
How should I change my query to do this? To get table T1 into the HumanResources schema?
Create Table T1
(
Id int,
Name varchar(20)
)
Create Table HumanResources.T1 (...);
In your attempt, you are trying to add it to a database called HumanResources and to the schema dbo. It's database.schema.object.
Edit
In response to the OP's comment, the question has already been answered here: How do I create a SQL table under a different schema?
The schema that will be used when schema is omitted will be the
default schema of the database user. Therefore, for creating table
without specifying schema, you'd have to set that database user's
default schema to dbo.
In your case try running:
CREATE TABLE [schemaname].[tableName](...)
Related
I want to create migration for updating table. So I want to check if columns exist or not in db table before executing to remove error of duplicate column. Is there any to check using db forge in CodeIgniter?
Thank you for help.
ALTER TABLE table_name DROP COLUMN IF EXISTS col1, COLUMN IF EXISTS col2;
dbforge used in Codeigniter to add or modify the tables.
$this->dbforge->add_column('table_name', $fields);
$this->dbforge->drop_column('table_name','column_name');
$this->dbforge->modify_column('table_name','columns');
We'd like to clone just the metadata. Currently, it seems snowflake just allows cloning with data. We'd like to clone just the objects with out data. is it possible ?
There are a few solutions to this
Clone and Truncate
CREATE TABLE myNewTable CLONE myTable;
TRUNCATE TABLE myNewTable;
Create Table As Select with a filter
CREATE TABLE myNewTable AS
SELECT * FROM myTable WHERE 1=0;
Generate the DDL for the table, and then run it
SELECT GET_DDL( 'table' , 'mytable' );
For a single table,
CREATE TABLE new_table LIKE old_table COPY GRANTS;
This question has been asked and answered multiple times, like here:
Clone Snowflake metadata only by Mike Walton.
Suggested answer for SCHEMA and DATABASE in link:
CREATE <object> new_obj CLONE old_obj
For all tables in new_obj: TRUNCATE TABLE obj_table
I am creating a SQL script that will check weather the object is there or not and then do create or alter based on that.
if not exists (select * from sysobjects where name='MyTableName' and xtype='U')
create table MyTableName(
Id int not null,
Name varchar(150) not null
)
Else
--Alter
Now having to do this for a bigger database which has more than 150 table.
Is there any way I can generate this automatically for tables and stored procedures?
As far as I can judge: Your question already includes the anser - NO it can not be done! The Thing is this: you COULD spool the metadata of tables etc. IF the objects existed in an Environment... as you arleady mentioned this is not the case - so how should a function / script / procedure be able to KNOW which columns etc. have to be created for which Table? Or what has to be altered if the table already existed?
Currently by mistake I created a table with the same name as a Database name in SQL server. Now I want to drop the table. How should I do that?
table name: dbo.employee
database name: dbo
Let your table name be same as DB name; that won't cause any issue cause to drop a table you use drop table ... command and to drop database drop database ... command. BTW, from your post that dbo rather looks like schema name.
I ran a ALTER SCHEMA ... on a table (SQL Server 2012 SP1) but the sys.objects record is still there. When I run the ETL I get an error that the table doesn't exist anymore because it's trying to remove all constraints. This one is of type PK Primary Key Constraint. How can I safely remove the record from the sys.objects table?
I managed to fix this issue by scripting the database to a new one, ALTER SCHEMA ... again on that table to bring it back to dbo., then DROP and recreate the table in the different table schema.