Click here to Skip to main content
14,692,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have a script checking and creating a HASH TABLE

IF EXISTS (SELECT keyid FROM #temp)
BEGIN
DROP TABLE #temp
END

SELECT * INTO #temp FROM [TABLE]
WHERE ID = 1593366


This works fine Microsoft SQL Server Management Studio, but when I try using this in my VB.NET Project, a runtime occur Invalid object name'#temp'. I even try this
IF EXISTS (SELECT keyid FROM #temp)
BEGIN
DROP TABLE #temp
END
but same happens


This is my code
If SQLConn.State = ConnectionState.Closed Then SQLConn.Open()
Dim SQLCmd As New SqlCommand
SQLCmd.Connection = SQLConn
SQLCmd.CommandText = SQL
SQLCmd.ExecuteNonQuery()


Please help, Thanks
Posted
Comments
Surendra Adhikari SA 26-Jun-13 10:13am
   
what value SQL holds at line SQLCmd.CommandText = SQL?
Mike Meinz 26-Jun-13 10:52am
   
Since a temp table is local to the context, there couldn't possibly be a temp table at the point in the code where you have the IF EXISTS(SELECT...) and DROP TABLE statements.

I do not understand why you need to create a temp table.
Why not use just SELECT * FROM [TABLE] WHERE ID = 1593366 in your VB .NET project?

Also, your first example fails in SQL Server Management Studio 2012 with the error:
Msg 208, Level 16, State 0, Line 1
Invalid object name '#temp'.
hansoctantan 28-Jun-13 5:33am
   
I just do this and it works
SELECT * INTO temp FROM [TABLE]
WHERE ID = 1593366
This code is just the first script in my SQL and theres a lot more

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900