In creating a new table in SQL Server 2005 - sql

I have a table Company. Now I need to copy the contents of table Company in a new table Employee (that too while creating the Employee table). I just used the query shown below, but its showing an ERROR.
Create Table Employee as (Select * from Company)
Is there any other solution for this....?

Simply use INTO:
Select * into Employee from Company

Related

Query emp table in oracle 19c

make a java program that queries the inbuilt table “emp” and display the first two columns (empno using column index and ename using column name ) of all the rows.
But problem here is :
ERROR:
ORA-04043: object emp does not exist
I am using oracle 19c on windows platform and using pluggable database orclpdb user "hr".
please tell me where to find this emp table.
And second question :
Even if I manually create a new Emp table in hr user ,what will be the sql command for the given
/*
* Using
* queries the inbuilt table “emp” and
* displays the first two columns
* (empno using column index and ename using column name )
* of all the rows.
*/
just tell me the sql command only.

Using a select statement to define a table name

I am trying to create snapshot tables as part of script I run regularly.
At the moment I have to manually enter table names, but I would like to call on a field in the base data table to create the snapshot table name.
For example:
Base Data Table = base_data and contains a field for the month it was created in.
Snapshot table = base_data_month
I have already tried to run this to create an automatically named table...
create table base_data_snapshot_||(select month from base_data) as
select * from base_data
But this gets a syntax error. For reference there is only one month included in the base data.
Has anyone had any success with this before?
Although we cant combine a running create statement with a select but can use select. So, I guess below should work
create table
base_data_snapshot_||t.month
as
( select * from base_data) t

Trying to append a query onto a temporary table

Using SQL Server 2008-R2
I have a csv of purchase IDs and in my database there is a table with these purchase IDs and there corresponding User IDs in our system. I need these to run a more complicated query after that using. I tried to bulk insert or run import wizard but I don't have permission. My new idea is to create a #temp using SELECT INTO and then have the query inside that like below.
SELECT *
INTO ##PurchaseIDs
FROM
(
SELECT PurchaseID, UserID, Added
FROM Users
WHERE PurchaseID IN (
/* These are the csv IDs just copied and pasted in */
'49397828',
'49397883',
etc.
What happens is that there are ~55,000 IDs so I get this error.
The query processor ran out of internal resources and could not
produce a query plan. This is a rare event and only expected for
extremely complex queries or queries that reference a very large
number of tables or partitions. Please simplify the query. If you
believe you have received this message in error, contact Customer
Support Services for more information.
It works if I upload about 30,000 so my new plan is to see if I can make a temp table, then append a new table to the end of that. I am also open to other ideas on how to accomplish what I am looking to do. I attached an idea of what I am thinking below.
INSERT *
INTO ##PurchaseIDs
FROM (
SELECT PurchaseID, UserID, Added
FROM Users
WHERE PurchaseID IN (
/* These are the OTHER csv IDS just copied and pasted in */
'57397828',
'57397883',
etc.
You need to create a temp table and insert the values in IN clause to the temp table and Join the temp table to get the result
Create table #PurchaseIDs (PurchaseID int)
insert into #PurchaseIDs (PurchaseID)
Select '57397828'
Union All
Select '57397828'
Union All
......
values from csv
Now use Exists to check the existence of PurchaseID in temp table instead of IN clause
SELECT PurchaseID,
UserID,
Added
FROM Users u
WHERE EXISTS (SELECT 1
FROM #PurchaseIDs p
WHERE u.PurchaseID = p.PurchaseID)

Import data from one DB to another Database with condition

I have no clue how to Import data from one database to another with condition.
I have DB Name (Northwind) and the table Name Employee
I have following columns
ID
Name
I have another DB (Mater) and the table Name Employee.
I have the following columns
Emp.ID
Emp.Name
Now i want transfer all data from Northwind.Employee to Master.Employee table with Condition.
Condition is
IF ID=1 then Emp.ID=201 (this is a constant value no logic behind that)
Any idea or suggestion please
in order to select from diferent DB you can assist this question:
INSERT INTO from two different server database
the id issue is a simple case you can see example here: SQL Case Statement Syntax?
if the databases are on the same server, you can just reference the 2 tables from the different databases with SQL such as the following:
INSERT INTO Master.Employee
SELECT 201 as ID, e2.Name
FROM Northwind.Employee e2
WHERE e2.ID = 1
But if the databases are on different servers, you will have to use either a linked server or SSIS package to achieve this.
If I'm understanding your question correctly, you can use a case statement in your insert:
insert into master.schema.employee (id, name)
select case when id = 1 then 201 else id end, name
from northwind.schema.employee

how to create a duplicate table with no data in it

how to create a duplicate table with no data in it
old tablename--student
newtablename--student1
plz tell me the exat query
thanks,
Churchill..
SELECT TOP 0 * INTO student1 FROM student
select * into student1 from student where 1=0
First you can copy the whole table by:
select * into (your_table_name) from student
After copying the whole table, you delete the data by running:
truncate table (your_table_name);
create table student1 as select * from student;
In the above query student1 is the table which has the exact copy of the already existing table student which copies the entire data.
create table student1 as select * from student where 0=1;
The query copies only the columns present in the student table but no data will be copied
But please note, you can't copy the constraints and indexes
select *
into student1
from student
where 1=2
This will get you the columns,
but indexes and other objects
will require scripting
with a database tool of some sort.
create table newtable as select * from oldtable where clause
In Sql Server you can right click on the table name in the Object Explorer.
Select "Script Table as" > "CREATE To" and then select "New Query Editor Window"
This creates a query to create the table.
In this case table name [student] change this to [student1] in the query and run and you have an exact copy of the schema, indexes, vartypes, primary keys etc.