not able to copy data from one table to another in sql server - sql

i am using new installed sql server 2008
i am trying to copy data from one table to another..i wrote query like this;
insert into Clr select * from Color_tbl
but this is showing error like this:
Invalid object name 'Clr'.
what i have to do? i have change any setting?
clr is not an existing table..i hop if i execute this query that will create clr table automatically...
I used to copy data from one table to another table like this: but i dont know what happend after installing new Sql server??
any help is very appriciable

Without knowing anything else about your database and schemas, you could try using fully qualified names, like so:
INSERT INTO [DBContainingClr].[SchemaContainingClr].Clr
SELECT * FROM [DBContainingColor_tbl].[SchemaContainingColor_tbl].Color_tbl
UPDATE: OP has clarified that he is in fact trying to CREATE the table Clr, and it doesn't exist at the moment. In which case the syntax should be:
SELECT * INTO Clr FROM Color_Tbl

Related

Incorrect syntax near 'REPLACE' with MSSQL

I have migrated an oracle database to Microsoft SQL Server via liquibase but there are still some SQL statements that don't work. This one looked like this in oracle:
CREATE OR REPLACE VIEW "BP_RESULTS_VIEW" (
BP_ID,
RES_NAME,
RES_LONG_NAME,
MEDIAN,
LOW_HINGE,
HIGH_HINGE,
H_SPREAD,
INNER_FENCE_LOW,
INNER_FENCE_HIGH,
OUTER_FENCE_LOW,
OUTER_FENCE_HIGH,
LOW_NOTCH,
HIGH_NOTCH,
LOW_ADJACENT_VALUE,
HIGH_ADJACENT_VALUE)
AS
SELECT
r.BP_ID,
rv.RES_NAME,
rv.RES_LONG_NAME,
r.MEDIAN,
r.LOW_HINGE,
r.HIGH_HINGE,
r.H_SPREAD,
r.INNER_FENCE_LOW,
r.INNER_FENCE_HIGH,
r.OUTER_FENCE_LOW,
r.OUTER_FENCE_HIGH,
r.LOW_NOTCH,
r.HIGH_NOTCH,
r.LOW_ADJACENT_VALUE,
r.HIGH_ADJACENT_VALUE
FROM
bp_results r,
results_view_display rv
WHERE
CAST (rv.value AS INT) = r.bp_id AND
rv.type = 'BOX';
After migrating it to Microsoft SQL Server it looks like this:
CREATE OR REPLACE FORCE VIEW BP_RESULTS_VIEW (BP_ID, RES_NAME, RES_LONG_NAME, MEDIAN,
LOW_HINGE, HIGH_HINGE, H_SPREAD, INNER_FENCE_LOW, INNER_FENCE_HIGH,
OUTER_FENCE_LOW, OUTER_FENCE_HIGH, LOW_NOTCH, HIGH_NOTCH,
LOW_ADJACENT_VALUE, HIGH_ADJACENT_VALUE) AS SELECT
r.BP_ID,
rv.RES_NAME,
rv.RES_LONG_NAME,
r.MEDIAN,
r.LOW_HINGE,
r.HIGH_HINGE,
r.H_SPREAD,
r.INNER_FENCE_LOW,
r.INNER_FENCE_HIGH,
r.OUTER_FENCE_LOW,
r.OUTER_FENCE_HIGH,
r.LOW_NOTCH,
r.HIGH_NOTCH,
r.LOW_ADJACENT_VALUE,
r.HIGH_ADJACENT_VALUE
FROM
bp_results r,
results_view_display rv
WHERE
CAST (rv.value AS INT) = r.bp_id AND
rv.type = 'BOX'
GO
But when I want to execute it always this error occurs:
Incorrect syntax near 'REPLACE'.
I don't understand why because the REPLACE statement exists in SQL Server too. It also seems like it doesn't recognize the CAST command. I am using Microsoft SQL Server Management Studio 17
In SQL Server 2016 SP1 and later (including Azure SQL Database), use CREATE OR ALTER VIEW for the equivalent functionality. In earlier SQL Server versions, one must first drop the view and then CREATE VIEW and GRANT permissions.
SQL Server doesn't support CREATE OR REPLACE VIEW.
Instead, create the view the first time. Then simply use ALTER VIEW. That is the simplest method. You can also drop the view and the re-create it.

ColdFusion generate code to create database table

Im currently building an app to help with day to day development of our app development team.
Im wondering is there any sort of easy way to generate code to generate SQL tables that have already been created for MSSQL ?
I ask this because in MSSQL you can right click a table and choose the generate scripts option and it will create the code neccessary to build that particular table ?
Is there any way via SQL to leverage that function, or anyway within ColdFusion to create this code, without having to write it from scratch ?
I would use something like this SQL server query to get the list of columns in a table along with Data types, NOT NULL, and PRIMARY KEY constraints to get the table names and columns and data types and construct something with the results to write a script for creating the tables.
You can right click and generate the script from SQL. Then in CF, you can have something like this:-
<cfquery name = "query1" dataSource = "ds1">
type in the generated script from SQL here
</cfquery>`

Linked server with dot in name

I have a query like this:
SELECT PID,surname,forenames,othername,date_of_birth FROM OPENQUERY(compact,'SELECT PID,surname,forenames,othername,date_of_birth FROM Order.PERSONS')
This is run from SQL Studio Manager. Order.Persons is an Oracle database. This query works as intended. Is it possible to do this:
SELECT PID,surname,forenames,othername,date_of_birth FROM OPENQUERY(compact.world,'SELECT PID,surname,forenames,othername,date_of_birth FROM Order.PERSONS')
i.e. the change the linked server name to compact.world.
Try using [compact.world] instead of compact.world.

How to Query Database Name in Oracle SQL Developer?

How do I query the database name in Oracle SQL Developer? I have tried the following and they all fail:
SELECT DB_NAME();
SELECT DATABASE();
Why do these basic MySQL queries fail in SQL Developer? Even this one fails too:
show tables;
EDIT: I can connect to the database and run queries such as:
select * from table_name_here;
EDIT 2: The database type is Oracle, this is why MySQL queries are failing. I thought it was related to the database client not the database itself. I was wrong. I'll leave the question as is for other as lost as I was.
Once I realized I was running an Oracle database, not MySQL, I found the answer
select * from v$database;
or
select ora_database_name from dual;
Try both. Credit and source goes to: http://www.perlmonks.org/?node_id=520376.
try this:
select * from global_name;
You can use the following command to know just the name of the database without the extra columns shown.
select name from v$database;
If you need any other information about the db then first know which are the columns names available using
describe v$database;
and select the columns that you want to see;
I know this is an old thread but you can also get some useful info from the V$INSTANCE view as well. the V$DATABASE displays info from the control file, the V$INSTANCE view displays state of the current instance.
Edit: Whoops, didn't check your question tags before answering.
Check that you can actually connect to DB (have the driver placed? tested the conn when creating it?).
If so, try runnung those queries with F5
To see database name,
startup;
then type
show parameter db_name;
DESCRIBE DATABASE NAME; you need to specify the name of the database and the results will include the data type of each attribute.

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.