Solved

SQL statement

Posted on 2009-04-10
8
216 Views
Last Modified: 2012-05-06
Hi,

How can I create a temp table that has a primary auto increment.

CREATE TABLE #TEMPPRJ
(
      TPPrjID INT PRIMARY KEY Auto_increment,
      TPProject char(24) NULL
)
it says incorrect syntax near Auto_increment.
 

there is more stuff to this question

Open in new window

0
Comment
Question by:lulu50
  • 4
  • 3
8 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24118521
Try like this:
CREATE TABLE #TEMPPRJ

(

      TPPrjID INT PRIMARY KEY IDENTITY(1,1),

      TPProject char(24) NULL

);
 

INSERT INTO #TEMPPRJ(TPProject)

SELECT 'Test'

UNION SELECT 'Another Test';
 

SELECT * FROM #TEMPPRJ;
 

DROP TABLE #TEMPPRJ;

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24118545
For your reading pleasure:
http://msdn.microsoft.com/en-us/library/aa258255(SQL.80).aspx

Best regards,
Kevin
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24118573
no points please...just something else to read about besides what Kevin posted.

http://sqlservernation.com/blogs/tipweek/archive/2009/02/23/identity-columns.aspx
0
 

Author Comment

by:lulu50
ID: 24118609
How can I append data to the temp table from a select statement say customer
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:lulu50
ID: 24118630
select * from #TEMPPRJ

output is:

TPPrjID   TPProject
01           58695
02           58659
03           59865

now I want to append to the temp table a column with data from
customer table

select customername from customer
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24118638
Change this statement:
INSERT INTO #TEMPPRJ(TPProject)
SELECT 'Test'
UNION SELECT 'Another Test';

To something like this:
INSERT INTO #TEMPPRJ(TPProject)
SELECT DISTINCT ProjectColumnName
FROM Customer
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24118657
If you mean, you want to join on project to get the customer name then do something like this:

SELECT t.*, c.customername
FROM #TEMPPRJ t
INNER JOIN Customer c ON c.TPProject = t.TPProject

OR something to that effect.  If you meant to insert new rows of TPProject values based on customername values in your customer table, see my original suggestion using INSERT INTO.
0
 

Author Closing Comment

by:lulu50
ID: 31569031
Excellent Thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mySQL Syntax 7 34
SYNTAX PROBLEM -- adding another column into the stored procedure output 30 32
SQL Query Syntax Join 4 34
ORA-00923: FROM keyword not found where expected 3 33
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now