This question already has answers here:
Check if table exists in SQL Server
(29 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
CREATE TABLE IF NOT EXISTS works on mysql but fails with SQL Server 2008 R2.
What is the equivalent syntax?
if not exists (select * from sysobjects where name='cars' and xtype='U')
create table cars (
Name varchar(64) not null
)
go
The above will create a table called cars if the table does not already exist.
Related
This question already has answers here:
Postgresql Update with join
(3 answers)
Postgresql updating query based on join
(1 answer)
Postgresql - use JOIN with UPDATE
(3 answers)
Update values using joins and where
(1 answer)
Closed 4 months ago.
I have large table with many column and I need to update whole table with select statement. How can I update whole table selecting from another table in postgress?
I Think This is what you need :
UPDATE table_a
SET info = table_b.info
FROM table_b
WHERE table_a.id = table_b.id
updated according to #a_horse_with_no_name
This question already has answers here:
SQL add a new column and its values only can be in several fixed options
(2 answers)
Closed 7 months ago.
For example: I have a column named Software, where I want only 2 entries either Hotel or Restro. Besides these two;Hotel and Restro, the column should not allow other data. I am trying to implement this database in Hotel Managemnet System through ASP.NET. So, is it possible in anyway in SQL Server?
You may use a check constraint in the create table definition:
CREATE TABLE yourTable (
Id INT IDENTITY PRIMARY KEY,
some_col VARCHAR(6) NOT NULL CHECK (some_col IN ('Hotel', 'Restro'))
-- rest of definition here
);
This question already has answers here:
Check if table exists and if it doesn't exist, create it in SQL Server 2008
(8 answers)
Closed 2 years ago.
I have two Databases db1 and db2. In those two databases I have a table name "Asset_table" with same columns and same table structure. But In Some client's db2 does not have the "Asset_table".
First I need to check "Asset_table" is available in the client database and if it is available then insert data from db1 to that client DB table.
Here is my query.
IF EXISTS (--here I need to check if the table is available in the db2---)
BEGIN
INSERT INTO db2.Asset_table( asset_id, name,qty,description)
SELECT asset_id, name,qty,description
FROM db1.Asset_table
END
Can any one sugest me an script for this?
Is this what you need?
IF NOT EXISTS(Select 1 from db2.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo' AND TABLE_NAME='Asset_table')
BEGIN
CREATE TABLE db2.dbo.Asset_table(asset_id int, name nvarchar(50),qty int,description nvarchar(150));
END
INSERT INTO db2.dbo.Asset_table( asset_id, name,qty,description)
SELECT asset_id, name,qty,description
FROM db1.dbo.Asset_table;
This question already has answers here:
Check if a column exists in SQLite
(17 answers)
Closed 5 years ago.
Hi Im using this query in sqlite
tx.executeSql('CREATE TABLE IF NOT EXISTS STU_REMAINDER( REG_NO VARCHAR(15), REM_ID INTEGER, REM_DATETIME DATETIME, DETAILS VARCHAR(500),STATUS INTEGER )');
I want to add column SCH_ID in this table, if not exist in this column. Thank you.
I prefer using a GUI Firefox addon Sqlite Manager to work with Sqlite database. You can download it from below url
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
You can view queries in it too when you add or alter table.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cannot SELECT from UPDATE RETURNING clause in postgres
I am using a PostgreSQL database which has a "company" table and a "country" table which each have a uniqueID.
I want a setup where if I try to add a company to the database, it only adds the company to the database with a new unique ID if it doesn't already exist.
insert into company (unique_id, company_name)
select 42, 'New Company Name'
from company
where not exists (select 1 from company where unique_id = 42);
See also here for a more general solution: Insert, on duplicate update in PostgreSQL?