How to drop a Particular column in a Table?
CREATE TABLE dbo.MyTable (column_a INT, column_b VARCHAR(20) NULL);
GO
ALTER TABLE dbo.MyTable DROP COLUMN column_b;
GO
EXEC sp_help MyTable;
GO
DROP TABLE dbo.MyTable;
GO
Related
I have a sql script creating views/tables/indexes and some selects all in the one script with GO statement after each table/view creation and i get errors.. It is possible hash them all in one script???
IF OBJECT_ID('dbo.ABC', 'U') IS NOT NULL
DROP TABLE dbo.ABC;
GO
select v.*
INTO dbo.ABC
from dbo.BC v
GO
CREATE NONCLUSTERED INDEX IX_ABC_ID ON dbo.ABC ([ID])
GO
GO
CREATE VIEW [dbo].[vABC] AS
SELECT [ID]
,[Description]
,[Name]
FROM [dbo].[NewDRGTable]
GO
You can use EXEC statement for each part like this (don't forget to escape apostrophe):
EXEC('IF OBJECT_ID(''dbo.ABC'', ''U'') IS NOT NULL
DROP TABLE dbo.ABC;')
etc.
I want to insert new column in existing table with 2nd position.
Now i have columns order like
Emp_id, Emp_Name, Address, phoneNo.
I want to add "Gender" in near Emp_Name.
Emp_id, Emp_Name, Gender, Address, phoneNo).
I can't delete this table and create new table.
You can not do this programmatically without creating a new table.
if you are allowed to create and delete tables then:
create a new table:
CREATE TABLE new_table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
...
);
move the data from your main table to the new one then delete the old table.
DROP TABLE my_table
after all rename the new table to the name of which was on your old deleted table.
--alternative:
you can reorder columns in your host application if possible!
Hi you may try the following:
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_TableName
(
Emp_id INT NOT NULL,
Emp_Name VARCHAR(100) NULL,
Gender BIT NULL,
[Address] VARCHAR(100) NULL,
phoneNo int NULL
) ON [PRIMARY]
GO
IF EXISTS(SELECT * FROM dbo.Tmp_TableName)
EXEC('INSERT INTO dbo.Tmp_TableName (Emp_id, Emp_Name, [Address],phoneNo)
SELECT Emp_id, Emp_Name, [Address],phoneNo FROM dbo.TableName WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.TableName
GO
EXECUTE sp_rename N'dbo.Tmp_TableName', N'TableName', 'OBJECT'
GO
COMMIT
Make sure to change TableName to your actual table name.
Giannis
You can do it but what for?
SQLFiddle demo
Alter table T ADD Gender varchar(10);
Alter table T ADD Address2 varchar(100),phoneNo2 varchar(100);
update T set Address2=Address,PhoneNo2=PhoneNo;
Alter table T DROP COLUMN Address,PhoneNo;
Alter table T ADD Address varchar(100),phoneNo varchar(100);
update T set Address=Address2,PhoneNo=PhoneNo2;
Alter table T DROP COLUMN Address2,PhoneNo2;
I am trying to select max: value from a table and insert value into same table.
The code is:
CREATE PROCEDURE [dbo].[InsertLogin]
#LOG_ID INT OUTPUT,
#LOG_NAME VARCHAR(100),
#LOG_EMAIL VARCHAR(100)
AS
INSERT INTO login(LOG_NAME, LOG_EMAIL)
VALUES(#LOG_NAME, #LOG_EMAIL)
SET #LOG_ID = ##IDENTITY
The other values are inserting except LOG_ID its getting null.
My guess is your Log_Id column in your Login table is not setup to be an Identity.
Through T-SQL, you have to drop and readd the column:
alter table login
drop column log_id
alter table login
alter column log_id int not null Identity(1,1)
Alternatively you can do this pretty easily in SSMS. Here's a decent article on the subject:
http://blog.sqlauthority.com/2009/05/03/sql-server-add-or-remove-identity-property-on-column/
I'd also recommend using SCOPE_IDENTITY() over ##Identity
I'm trying to run the following SQL statement:
IF OBJECT_ID('MyTable') IS NOT NULL
DROP TABLE MyTable
SELECT
a.UserId
INTO
MyTable
FROM
UsersTable a
WHERE
a.UserId='12359670-1DC9-4A0A-8AE5-29B664C1A57E'
ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL
ALTER TABLE MyTable ADD PRIMARY KEY(UserId)
However, I get the following error:
Cannot define PRIMARY KEY constraint on nullable column in table 'MyTable'.
Any ideas?
This assumes SQL Server based on UNIQUEIDENTIFIER
Put a GO between (or relevant batch separator if not SQL Server)
....
ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL
GO
ALTER TABLE MyTable ADD PRIMARY KEY(UserId)
At batch compile time, the column is nullable. So break up the batches.
SQL isn't a line by line procedural language
You'll have to do this in a stored procedure
ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL
EXEC('ALTER TABLE MyTable ADD PRIMARY KEY(UserId)')
Found a solution.
I'm using the following statement:
EXEC sp_ExecuteSQL N'ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL'
EXEC sp_ExecuteSQL N'ALTER TABLE MyTable ADD PRIMARY KEY(UserId)'
You don't need dynamic SQL. Just create the table and the key first.
CREATE TABLE MyTable (UserId UNIQUEIDENTIFIER NOT NULL PRIMARY KEY);
INSERT INTO MyTable (UserId)
SELECT UserId
FROM UsersTable
WHERE UserId='12359670-1DC9-4A0A-8AE5-29B664C1A57E';
Here is my code:
alter procedure test1 as
select DeptID,DeptName from Department
go
alter procedure test2 as
--Create Table #tab (DeptID INT, DeptName VARCHAR(255))
INSERT INTO #tab
exec test1
select * from #tab
drop table #tab
go
exec test2
I am getting an error like "Invalid object name #tab"
If I add at the begining Create Table #tab (DeptID INT, DeptName VARCHAR(255)) then I do not get any error.
What is wrong in my code? Can I populate a temp table from the results of a stored procedure without declaring the temp table and its column definitions?
When loading a temp table from a stored procedure, then you have to CREATE the table first.
There is no straightforward equivalent of
SELECT * INTO #temptable FROM AnotherTable
The non-straightforward version (read all about the bad stuff on "How to Share Data Between Stored Procedures". And simpler) would be
SELECT * INTO #temptable FROM OPENQUERY(Loopback, 'exec test1')
It's because the Local Temporary table #tab which you are expecting does not existing in the session.
So the table creation should not be commented line.
Create Table #tab (DeptID INT, DeptName VARCHAR(255))
Moreover, if you want to do without creating the table then it should be like below
Alter procedure test2
As
Set NoCount ON
IF OBJECT_ID('tempdb..#tab') IS NOT NULL
Begin
Drop table #temp
End
SELECT DeptID, DeptName INTO #tab from Department
Select * from #tab
Drop table #tab
Go