I am trying to get this command that is on auto run to have an automatic delete incase I have to re-run the application. I am using visual studio and sql server 2012.
Here is what I have. The Create table works but its the IF EXISTS that I am having trouble with.
IF EXISTS (DROP TABLE ST_BANLIST)
CREATE TABLE ST_BANLIST
(BAN VARCHAR (9).
CALL_ACTIVITY CHAR(1).
BAN_STATUS CHAR(1))
Thanks for any help
Your syntax was incorrect:
IF OBJECT_ID('dbo.ST_BANLIST', 'U') IS NOT NULL
DROP TABLE dbo.ST_BANLIST
SQL Server 2016 makes this a lot easier (what took so long Microsoft?):
DROP TABLE IF EXISTS dbo.ST_BANLIST
if exists(select * from sys.objects where name ='MytableName' and type='U')
Drop table dbo.MytableName
Related
I am using SQL Server 2012. When I want to create a temporary table named #TBL1, or rerun my code, I get this error:
There is already an object named '#TBL1' in the database
So I added this code to my query:
IF OBJECT_ID('dbo.#TBL1', 'U') IS NOT NULL
DROP TABLE dbo.#TBL;
But it shows the same error message
Please give me a clue is to what is wrong
You need to make sure OBJECT_ID looks in the right place. Temporary tables live in tempdb:
IF OBJECT_ID('tempdb.dbo.#TBL1', 'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#TBL;
END
Also seems there is a typo (#TBL1 vs. #TBL).
And while I am normally am a big fan of schema prefixes, for #temp tables it’s not necessary and actually hampers readability IMHO.
When I create a temporary table, I usually make sure that if they exist, I drop them.
IF OBJECT_ID(N'tempdb..#tempTable') IS NOT NULL
DROP TABLE #tempTable
I recently realized that the following method does the same:
DROP TABLE IF EXISTS #tempTable
Is there one way better than the other ?
Explanation
They do the same thing just different syntax, and the later method in your question is newer. The IF EXISTS clause has been supported with DROP TABLE since SQL Server 2016 13.x up through the current version as of writting this, SQL Server 2019 (15.x).
Documentation
The IF EXISTS functionality is documented in the arguments section here: https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-table-transact-sql?view=sql-server-ver15#arguments
I have a migration script with the following statement:
ALTER TABLE [Tasks] ALTER COLUMN [SortOrder] int NOT NULL
What will happen if I run that twice? Will it change anything the second time? MS SQL Management Studio just reports "Command(s) completed successfully", but with no details on whether they actually did anything.
If it's not already idempotent, how do I make it so?
I would say that second time, SQL Server checks metadata and do nothing because nothing has changed.
But if you don't like possibility of multiple execution you can add simple condition to your script:
CREATE TABLE Tasks(SortOrder VARCHAR(100));
IF NOT EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE [TABLE_NAME] = 'Tasks'
AND [COLUMN_NAME] = 'SortOrder'
AND IS_NULLABLE = 'NO'
AND DATA_TYPE = 'INT')
BEGIN
ALTER TABLE [Tasks] ALTER COLUMN [SortOrder] INT NOT NULL
END
SqlFiddleDemo
When you execute it the second time, the query gets executed but since the table is already altered, there is no effect. So it makes no effect on the table.
No change is there when the script executes twice.
Here is a good MSDN read about: Inside ALTER TABLE
Let's look at what SQL Server does internally when performing an ALTER
TABLE command. SQL Server can carry out an ALTER TABLE command in any
of three ways:
SQL Server might need to change only metadata.
SQL Server might need to examine all the existing data to make sure
it's compatible with the change but then change only metadata.
SQL Server might need to physically change every row.
Can you add a column to a table inserting it in between two existing columns in SQL Server without dropping and re-creating the table?
Mediumly long answer, yes (ish) but it's ugly and you probably wouldn't want to do it.
please note: this code creates a physical table
CREATE TABLE MyTest (a int, b int, d int, e int)
INSERT INTO MyTest (a,b,d,e) VALUES(1,2,4,5)
SELECT * FROM MyTest
ALTER TABLE MyTest ADD c int
ALTER TABLE MyTest ADD d_new int
ALTER TABLE MyTest ADD e_new int
UPDATE MyTest SET d_new = d, e_new = e
ALTER TABLE MyTest DROP COLUMN d
ALTER TABLE MyTest DROP COLUMN e
EXEC SP_RENAME 'MyTest.d_new', 'd';
EXEC SP_RENAME 'MyTest.e_new', 'e';
SELECT * FROM MyTest
DROP TABLE MyTest
The simple answer is no. Is there a reason why column order is important to you?
Yes you can add here is the query for your concern:
ALTER table table_name ADD column column_name(new) datatype AFTER column_name
Take a look at this link:
http://www.bobsgear.com/display/ts/Adding+Column+After+Another+Column+-+SQL+Server+2005
As you can see, the answer is:
'not possible without moving data to a temp table'
which is what the SQL Server Management Studio actually does.
yes. You can drag and drop it in design mode, or copy and paste it in edit table mode
in response to comments, yes, this is with SQL Server Management Studio through the UI, and under the hood, it's doing the drop/recreate you're trying to avoid, so no, you can't do it without the server doing it for you.
This is possible in SQL Server Management Studio (SSMS).
Filter your table in Object Explorer and right click on the table > Design
Drag the arrow highlighted in the left to move your column.
First answer, no.
Second answer, according to this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58912, found via Can I logically reorder columns in a table?, you could create the column then reorder the columns in the table by editing the system tables. But it looks incredibly messy and I would not trust that things would not break.
For Mysql yes For SQL server No
also it's not a good practice to add column in between as it can hamper some queries if you are relying on the table schema in your project.
i use the statement drop trigger if exist TRIGGER in sqlite but sql server doesnt like the if statement. (i guess exist is the offending word). I do this right next to my create trigger statement because i want to drop older triggers with the same name so i can replace it with this new one.
How do i do this in SQL server?
in SQL Server Management Studio (and, I think in Query Analyzer) right-click the trigger in the explorer, and choose the Script-as option, choose 'Drop Trigger' in clipboard, and SSMS will create the T-SQL syntax for you to drop that trigger.
Sorry I haven't given you T-SQL you can copy and paste, but this way you'll know how to do it for next time.
You can check for the existence of a specific Trigger like so.
IF EXISTS
(
select name
from sys.objects
where type='TR' and name ='Trigger Name'
)
BEGIN
--Add your Trigger create code here
END
I find this to be a more compact SQL Server equivalent to MySQL's DROP TRIGGER IF EXISTS syntax:
IF OBJECT_ID('XXXX', 'TR') IS NOT NULL
DROP TRIGGER XXXX
I'd use something like:
IF objectproperty(object_id('dbo.xxx'), 'isTrigger') = 1
DROP PROCEDURE dbo.xxx
GO
CREATE TRIGGER dbo.xxx [etc]
replacing xxx with your trigger name (and dbo with the relevant schema, if necessary).
Alternatively, you could just use
ALTER TRIGGER dbo.xxx [etc]
Since version 2016 this syntax is also supported by Microsoft SQL Server:
DROP TRIGGER IF EXISTS trigger_name