Copy all columns without data but with dependencies in SQL [duplicate] - sql

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 5 years ago.
So i'm trying to copy all data of table CarOrders into a new table, NewCarOrders. But i only want to copy columns and dependencies and not data. I don't know if it truly matters, but i'm using SQL Server Management Studio.
Here are two things that i tried, but none worked.
SELECT *
INTO NewCarOrders
FROM CarOrders
WHERE 0 = 1
The issue with above is, it is copying all the columns but it is not copying the dependencies/relationships.
I saw some posts on some of the forums regarding this, but the suggested solution was using SSMS Wizard. I want to do it using SQL (or T-SQL).
I'm new to SQL world, so i'm really sorry if it is a stupid or no brainer question. Let me know if i am missing some important information, i'll update it.

Try below query and check if this works for you
SELECT TOP 0 *
INTO NewCarOrders
FROM CarOrders
This will create NewCarOrders table with same structure as CarOrders table and no rows in NewCarOrders.
SELECT * FROM NewCarOrders -- Returns zero rows
Note : This will not copy constraints , only structure is copied.
For constraints do as below -
In SSMS right click on the table CarOrders, Script Table As > Create To > New Query Window.
Change the name in the generated script to NewCarOrders and execute.
Also change the constraints name in the generated script else it will throw error like There is already an object named 'xyz' in the database

You can use the like command:
CREATE TABLE NewCarOrders LIKE CarOrders;

Related

SQL Pivot table?

Hi I am looking for a SQL Script the can pivot the following table
Every values in ObjectClassProperty column must become columns. ObjectInstanceProperty will become values for the respectivly newly created column.
One thing to keep in mind. ObjectClass is dynamically changing.
I have implemented once such similar solution in MS SQL Server 2019 version.
Details can be found in the dbfiddle<>example<>dynamicpivot.
Note: This will work if there are not duplicates in ObjectClassProperty.

Best way to duplicate data in the same table and updating a column at the same time

I'm trying to find the best way to duplicate all the rows in a table.By this i mean inserting them again in the same table, but i need to update a single column only on the values that were inserted.
This is to help me write a script so I can automate some work on some clients.
I can't use select * as it will throw an error because of the identity columns but at the same time i don't want to be manually writting all the column names for several tables.
Is there a simple way to translate this into SQL server?
Sorry for not showing a piece of code, but i have nothing at the moment and i'm not really fluent in SQL.
EDIT: I have ended up following the advice of JamieD77 in the comments below this post by moving everything to a table, drop the id column, updating what i need and then moving back as it seems to be the most effiecient.
In SQL Server Management Studio you can drag the "Columns" folder under a table and drop it on the query window and it will paste a comma-delimited list of all the columns.
Or run a query like:
select string_agg(quotename(name),', ')
from sys.columns
where object_id = object_id('MyTable')

SQLite: How to add a column to all the tables in the database?

I am quite new to SQLite (SQL overall). I created a database and created multiple tables in it and suddenly realized I had forgot to add a column to them. Now I have 280 tables having distinct names without a column. I have tried adding * and combining with SELECT but it gives an error. How can I accomplish it?
my code:
ALTER TABLE "MYBUS.*" ADD COLUMN STOPID int null;
After 3 hours issue finally solved.
After searching for sometime, I stumbled upon this answer
SQlite alter table with result from select statement
I ran the code:select 'alter table '||tbl_name||' add STOPID int null' from sqlite_master
It returned all the 280 SQL queries needed but with quotation marks, so I took the clue from this.lau and created a python regex script and ran it. Worked like a charm and within 10 lines of code I was able to add a column to all my 280 tables.
See this link https://dba.stackexchange.com/questions/160145/add-columns-to-all-tables-in-a-database-if-the-columns-dont-exist
Even though it applies to SQL Server, this should help you in getting started.
Ensure that you perform a check on the column name before adding it to a table.

Specify name of table types in SQL Server [duplicate]

This question already has an answer here:
SQL Server create User-Defined Table Types with schema not working properly
(1 answer)
Closed 9 years ago.
Create a new database in MS SQL Server 2008 R2 and then create a new table type with the following command
CREATE TYPE typeMyType AS TABLE (ID INT)
Now execute the following query
SELECT OBJECT_NAME (object_id) AS ObjectName, *
FROM sys.indexes
WHERE index_id <= 1
ORDER BY ObjectName
This will show you that an object of type HEAP was created which is fine as the data for typeMyType has to be stored somewhere.
But the object is called TT_typeMyType_01142BA1 in my case.
Question:
Why isn't it called typeMyType and how can I overwrite this obviously server generated name?
The table type is listed in several places that can be looked at via the following system tables (sys.sysobjects, sys.indexes, sys.table_types and sys.types).
I can understand that table_types is a subset of types. Therefore, those two places are where to look for my table type AssociativeArray in my AdventureWorks2012 database.
The question I have is why is it looking like a table in sysobjects and sysindexes?
We have not defined any use for the variable yet. However, it looks like a table. It must be how the engine defines meta data for future use.
One take away, does the information in sysobjects and sysindexes get updated a run-time when we declare a variable of type AssociativeArray?
Also, what happens when two SPIDS create the same variable at the same time with different data being inserted?
That is a in-depth engine question that maybe a someone from Microsoft CAT team might know off the top of their head.
I guess I could do some research to find out.
Enclosed is a link stating that table variables use tempdb. That is what I know is a fact. Again, I wonder if the sys.objects and sys.indexes get updated or just place holders.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html

SQL Server 2008: copy table structure, and schema

thanks for your time. i edited my script, ran it, and still got this name: srp.dbo.gstDataCutover. i used to be able to do this easily with MSSQL2005. we've recently upgraded to 2008. and i dont remember doing it any other way...
Hi,
I'm trying to copy a table structure (columns, datatypes, schema) into a new table to have the same schema and structure, using the sql code below.
SELECT dbo.gstData.*
INTO [dbo.gstDataCutover]
FROM dbo.gstData
WHERE dbo.gstData.gstID < 1
My problem is, when i run this script the new table dbo.gstDataCutover is named as "dbo.gstDataCutover" but the schema is defaulted to the system schema ("srp"), which is actually srp.[dbo.gstDataCutover].
I want to copy both the structure and the schema.
Thanks!
Without any periods, the hard brackets indicate table name -- it's including the "dbo." in your example as part of the table name.
If you want the table created in the dbo schema:
SELECT t.*
INTO dbo.gstDataCutover
FROM dbo.gstData t
WHERE t.gstID < 1
Likewise, if you want the table created in the srp schema:
SELECT t.*
INTO srp.gstDataCutover
FROM dbo.gstData t
WHERE t.gstID < 1
The table name doesn't have any unusual characters, so there's no need to use hard brackets...
You can download the community edition of Visual Studio, which has features for comparing schemas as well as data. It will list the differences and allows you to select a set of changes, for which it will generate an update-script.