If I run the following SQL in SQL Server CE,
alter table audit add new_key nvarchar(100) null
Can that be changed to check whether the column exists before trying to add it in the same SQL statement? I am aware you can do that quite easily in SQL Server, but SQL Server CE?
If you are looking for something like you can do with SQL server: IF NOT EXISTS (..., unfortunately, this is not possible with SQL Server Compact (the T-SQL IF is not supported). See SQL Reference (SQL Server Compact).
Based on this thread, you can do something like this:
select * from Information_SCHEMA.columns
where Table_name='audit' and column_name='new_key'
(ie no result = does not exist).
Also note that batch queries are not supported in SQL Server Compact at all. So you can't check if the column exists and add the column if needed in the same SQL statement.
Or if you are looking for a snippet that you can use with VB.NET or C#, take a look at this SO question: How can I determine whether a column exists in a SQL Server CE table with C#?
The easiest way would be to query information_schema and see if the column is in there.
IF NOT EXISTS(
SELECT * FROM information_schema.columns
WHERE Table_Schema = 'dbo'
AND Table_Name = 'audit'
AND Column_Name = 'new_key'
) BEGIN
alter table audit add new_key nvarchar(100) null
END
Related
may i know how do you check if a database table already exist in the database already or not with just using the sql query method.
Try this
IF Exist(Select 1 from INFORMATION_SCHEMA. COLUMNS where TABLE_NAME like '%tableName%')
How can I find a table in SQL Server 2016?
I could use this to find the table in the current database:
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
The problem is, I have several databases on the server.
Please try with the below sql query.
select * from [YourDatabaseName].INFORMATION_SCHEMA.TABLES where TABLE_NAME LIKE '%App_Current_Seq_Num%'
sp_tables , sys.tables , information_schema.tables will only give you the list of tables from current selected database. Use the below undocumented stored procedure to run this code for each database, with each resultset showing the list of tables belonging to it.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%App_Current_Seq_Num%'''
Another option is to use dynamic SQL .
You can either do
use your_database_name
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or
SELECT *
FROM your_database_name.sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or if you want to search multiple databases at once use a union all clause.
Something like this:
SELECT * FROM database1.sys.Tables
UNION ALL
SELECT * FROM database2.sys.Tables
I used this and it worked perfectly:
USE Master
GO
EXEC sp_MSforeachdb
#command1='use ?; SELECT *
FROM sys.tables
WHERE name LIKE ''%App_Current_Seq_Num%'''
you can use SQL Search
it is a simple search control that you can use in sql server
to find tables/views/stored procedures and etc...
use fast free download from
https://www.red-gate.com/products/sql-development/sql-search/index
What is the equivalent SQL Server 2012 code for this please ?
IF INSERTING THEN
:NEW.audsid:=SYS_CONTEXT('USERENV', 'SESSIONID');
Oracle and SQL Server handle triggers very differently. Oracle has the concept that a trigger affects only one row at a time. SQL Server doesn't. Instead, it uses a "table" inserted with the new rows.
So, your question has three parts:
What is the equivalent of INSERTED?
What is the equivalent of SYS_CONTEXT('USERENV', 'SESSIONID')?
What is the best way to do this in SQL Server?
Here is the answer to the first two questions:
if (exists (select 1 from inserted) and (not exists (select 1 from deleted))
update inserted
set audsid = ##SPID;
However, in most cases, you would just use the default keyword in the column definition:
audsid int default ##SPID
Much easier and a trigger isn't needed.
What is the equivalent of the oracle query SET UNUSED (Column_name) in SQL server?
ALTER TABLE table_name
SET UNUSED (column_name);
Also am looking for SQL equivalent for the dropping Unused column
ALTER TABLE table_name
DROP UNUSED COLUMNS;
Could someone please let me know the SQL equivalent of the above oracle statements.
Sorry, apples and oranges.
There is no equivalent statement in SQL Server.
SQL Server doesn't have a mechanism for marking columns as "unused," which is an Oracle extension of SQL.
I am trying to write an SQL query which needs to be compatible on both a Sybase and Oracle database. The query looks like the following :
SELECT *
INTO new_table
FROM other_table
This query is working great on a Sybase database but not on an Oracle one. I found the equivalent for Oracle :
CREATE table new_table AS
SELECT *
FROM other_table
Is there a way to write a third query that would do the same and that can be executed on a Sybase and on an Oracle database?
As you found, Oracle supports INTO but doesn't use it like Sybase/SQL Server do. Likewise, Sybase doesn't support Oracle's extension of the CREATE TABLE syntax.
The most reliable means of creating a table & importing data between the systems is to use two statements:
CREATE TABLE new_table (
...columns...
)
INSERT INTO new_table
SELECT *
FROM OLD_TABLE
Even then, syntax is different because Oracle requires each statement to be delimited by a semi-colon when TSQL doesn't.
Creating a table & importing all the data from another table is a red flag to me - This is not something you'd do in a stored procedure for a production system. TSQL and PLSQL are very different, so I'd expect separate scripts for DML changes.
There is no query that would do what you want. These environments are very different.
It is possible.
SELECT * INTO new_table FROM existing_table;
Thanks