sql server: create table taking forever! - sql

i am doing this:
CREATE TABLE person
(
num INT NOT NULL ,
firstname VARCHAR(20) NULL ,
lastname VARCHAR(30) NULL
);
sql server is just saying "debbuging" and it's not doing anything
is there a problem with the syntax or what?

You clicked on the 'Debug' button (green triangle) instead of 'Execute' (red exclamation)? SSMS is waiting on you to step through the T-SQL lines.

Check if there are locks on the database that would prevent SQL Server from executing a create statement. Since you're creating a new table, check for a database-level locks.

i will suggest the obvious...try restarting sql server?

Related

Python pyODBC : Issue inserting into a sql server table with an identity column

An INSERT statement that was created with Python gives me an error when I execute and commit it. I have taken a copy of the statement from Python and run it myself in SQL SERVER and it works fine there. The table I am trying to insert into has an identity column. When Python trys to execute it will give me an error saying what is below when I exclude the identity column in the statement
Table looks like this MY_TABLE ( ID INT IDENTITY(1,1) NOT NULL, A INT, B INT)
INSERT INTO MY_TABLE (A, B) VALUES(VALUE_A, VALUE_B);
"('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert the value NULL into column 'MY_IDENTITY_COLUMN', table 'MY_TABLE'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)")"
But when I try to include the value for the Identity column (I don't want to do this) I get the following error which makes sense as it's an identity column that we let the table auto-increment
"Cannot insert explicit value for identity column in table 'MY_TABLE' when IDENTITY_INSERT is set to OFF."
When I run the query in SQL SERVER the table fills the value for the Identity Column itself and auto-increments but for some reason when I run the statement in Python it does not do this and tries to pass a NULL
SQL SERVER version: 10.50.6560.0
Recognising it's a little late but... I had the same problem recently using some old program and ODBC. The solution was to create a View in SQL Server with only the columns required (i.e. A and B in your case) and then insert into that View.
A little hard to tell without your code, but here is an example.
In database create a test table
CREATE TABLE dbo.test(
ID INT IDENTITY NOT NULL PRIMARY KEY,
Name VARCHAR(40) NOT NULL
);
Then in Python specify the columns you are inserting into
import pyodbc
warecn = pyodbc.connect("Your Connection Stuff")
Inscursor = warecn.cursor()
Inscursor.execute("Insert into dbo.test(name) values ('This'), ('is'), ('a'), ('test')")
Inscursor.commit()
Inscursor.close()
del Inscursor
warecn.close()

SQL Server 2012 temp table OBJECT_ID issue

We have an issue upgrading to SQL Server 2012. I am using the following script to create temporary tables that used to work fine on SQL Server 2008 R2 but now it is generating an error with 2012:
if (OBJECT_ID( 'tempdb..#idstable') > 0)
truncate table #idstable
else
create table #idstable (id int not null)
the error thrown is
There is already an object named '#idstable' in the database.
This is obviously not thrown the first time I use the script (in the same transaction).
Any idea? Thank you!
In SQL Server 2012, #temp tables are created with a negative object_id, so your script won't work as is. The safest way is:
IF OBJECT_ID( 'tempdb..#idstable') IS NOT NULL
(I blogged about this here, and knew it would catch someone.)
Though your script is bound for failure anyway, if it is part of a single batch. The parser will not let you try and create the same #temp table twice.
Try this:
IF OBJECT_ID (N'tempdb..#idstable', N'U') IS NOT NULL
truncate table #idstable
else
create table #idstable (id int not null)
My dears,
This issue is due to the truncate statement. Truncate is used to delete all records preserving the table. Use drop table instead of truncate table and this will works fine ;-)

SQL Import/Export wizard not allowing Create Table #temp

Through the Import/Export wizard (2008 R2) I'm trying to get data from a View and do some joins with it and put the data in a temp table then do a final select statement at the bottom.
But I keep getting a message saying that my first temp table definition is invalid.
Here's the def:
Create Table #CT (Code int, Col1 varchar(75), Col2 varchar(75), Col3 int)
INSERT INTO #CT
SELECT *
FROM...
I know this is good because I can run it directly on the server without problems.
Is it that Imp/Exp wiz doesn't allow these kinds of queries, where there's complex statements?
At the top of your SQL code, try adding set fmtonly off.
In some circumstances, SQL Server tries to determine the metadata for a query without actually running the query... but, this doesn't play well with temp tables. Adding set fmtonly off instructs it to actually execute the query to get the metadata.

Stored Procedures and Triggers in data base

what do Stored Procedures and Triggers in data base mean ?
how can i create Stored Procedures ?
how can i crest Triggers ?
if you have simple examples for each of these .please help :)
what i know is only about trigger which is activated if an action of(insert or delete or update ) violates the constrains specified but i don't know how to create ,so again if any have example please
Think of a Stored Procedure as a method in your code. It runs a specific set of instructions.
Stored Procedures are created to, for example, manage complex sets of data that would normally be a pain to handle along in your code.
You can create a Stored Procedure with the following instructions:
Oracle
CREATE OR REPLACE PROCEDURE P_PROCEDURE_NAME (
pParameter1 NUMBER
, pParameter2 VARCHAR2(100 Bytes)
) AS
BEGIN
-- Procedure code here...
END;
SQL Server
CREATE PROCEDURE cspProcedureName
#parameter1 int
, #parameter2 nvarchar(100)
AS
-- Procedure code here...
Oracle
As for the Triggers, they are sets of code called upon an action occuring to the related table. For instance, in Oracle, there are no INDENTITY columns such as SQL Server offers. Instead, Sequences are used along with Triggers to simulate the same. Hence, you will need to create an Oracle SEQUENCE, then the TRIGGER to update the ID field of your table.
CREATE SEQUENCE SEQ_CUSTOMERS
MINVALUE 1
MAXVALUE 65535
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER TRG_CUSTOMERS_INSERT
BEFORE INSERT
ON TBL_CUSTOMERS
FOR EACH ROW
BEGIN
:NEW.CUST_ID := SEQ_CUSTOMERS.NEXTVAL;
END;
SQL Server
A trigger example in SQL Server would be updating automatically the update datetime of a record. Consider the following:
CREATE TABLE Customers (
CustId int NOT NULL IDENTITY(1, 1) PRIMARY KEY
, CustName nvarchar(100) NOT NULL
, CreatedOn datetime DEFAULT GETDATE()
, LastUpdate datetime NOT NULL
)
GO
CREATE TRIGGER trgCustomersUpdt
AFTER UPDATE
ON Customers
AS
update Customers
set LastUpdate = GETDATE()
where CustId = inserted.Custid
GO
DISCLAIMER
This code has not been tested and may require minor changes for it to work properly against its respective RDBMS.
To sum it up, Triggers are mainly used to as illustrated here, despite there are many other possible use, such as building up an history of table changes that occured throught time, keeping all records of transactions into an history table or the like. The Stored Procedures are mainly used to perform complex database tasks where this would get too complex to do in code.

In SQL Server 2005, how can I use database_b, do something, then use the old db database_a in TSQL?

In SQL Server 2005, how can I use database_b, do something, then use the old db database_a in TSQL? The following is my code but there is some problem with it. Who can help me to identity the problem? Great thanks.
DECLARE #old_database_name VARCHAR(200)
SET #old_database_name = db_name()
use mydatabase
create table t1(id int identity(1,1))
use #old_database_name
You'll need to use dynamic sql to do this.
e.g.
-- Do stuff in current DB here
EXECUTE ('USE mydatabase; create table t1(id int identity(1,1));')
-- Do more stuff in current DB here. This context will not have changed since before the EXECUTE statement
Why are you even using USE statements for this?
create table mydatabase.dbo.t1(id int identity(1,1))
This is similar to AdaTheDev's "EXECUTE" example, but uses sp_executesql :
-- Do stuff in current DB here
EXECUTE mydatabase..sp_executesql N'create table t1(id int identity(1,1));'
-- Do more stuff in current DB here. This context will not have changed since before the EXECUTE statement