I apologize if this has been asked, and answered, before. I wasn't able to find relevant posts on the forum. A Google search pointed me to a couple of pages that offered this simple recipe:
if object_id('#temp') is not null
print 'table exists';
(I have also tried 'tempdb.#temp', and 'object_id(..,'U'))
Unfortunately, this doesn't work: the condition evaluates to 'false'. #temp is alive and well: strying to 'select .. into' it raises error
> There is already an object named '#temp' in the database.
How can I check if a temporary table exists in the current context?
With normal tables I'd do a
EXISTS ( SELECT name FROM sysobjects
WHERE name='myTableName' AND type='U')
However, I can't do that with a temporary table. I'd have to go look at the sysobjects table in the tempdb database.
The problem is that for temporary tables, a suffix is added to the name to make it unique for each scope. I can't change the WHERE clause to name LIKE 'myTableName%' because this would return true if a temporary table with the same name exists in a different scope.
It is possible for global temporary tables, but for local, it is said that the names(in the tempdb) change each time the table is created, so i am not sure if there is a way to check it.
Hi all,I am in the process of creating a page that checks to see if a particular user exists in the database and if it does then send to welcome.aspx if not then accessdenied.aspx. The userid is requested from the query string.I have done some research and cannot find anything directly related, I have tried to add bits of code into what i think is the right place but I dont think what i am doing is correct. Can someone help and show me where my code is going wrong? Also is there a better/more efficient way to do what I am doing?Thanks in advance. default.aspx.csusing System;using System.Data;using System.Data.SqlClient;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string UserID = Request.QueryString["uid"]; //string TransferPage; if (UserID != null) { //initiate connection to db SqlConnection objConnect = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString); string sql = "SELECT COUNT(*) FROM members WHERE UserID = '" + UserID + "'"; SqlCommand query = new SqlCommand(cmd, test); int count = (int)query.ExecuteScalar(); int aantal = -1; // some default value if can't insert record if (count == 0) // no existing record for username { Server.Transfer("accessdenied.aspx"); } else { Session["UID"] = UserID; Server.Transfer("welcome.aspx"); } } }}
This my first time using the link server to read the files directly from within SQL2005, so issues the following link:-
EXEC sp_addlinkedserver @server = N'MYVFPSERVER', -- Your linked server name here @srvproduct=N'Visual FoxPro 9', -- can be anything @provider=N'VFPOLEDB', @datasrc=N'"C:PROGRAM FILESMICROSOFT VISUAL FOXPRO 9Samplesdata estdata.dbc"'
After that i can open query and do the import issues, but how can check if the table exists before issues the query.
What’s the easiest way to check if a single value exists in a table column? I’m building a simple login page, and I want to get the username and check if it exists in my Users table. Here’s my SQL: SELECT UserID FROM UsersWHERE UserID = "admin" Could someone give me code to check for “admin” in my UserID column? Here’s some code I tried, using my SqlDataSource, but it returns an error “Could not load type 'System.Data.OleDb'” protected void Button1_Click(object sender, EventArgs e) { // Retreive the results from the SqlDataSource as a DataReader OleDbDataReader reader = (OleDbDataReader) SqlDataSource1.Select(DataSourceSelectArguments.Empty); // Read in the value if (reader.Read()) { } // Close the reader reader.Close(); } I don’t have to use the SqlDataSource, but originally thought it might be easier. I know how to use SqlDataSource to fill a whole GridView but this is different.
Basically, i am still relatively new to ASP.net and SQL. And i have the following query. I have a skills table to which the user enters their skills using the following fields: Skillcatagory, SKill, Current Level, Target Level, target date and comments and the serial of the user. I need to check via our staff table, which people have had a skill entered for them. And then produce a report on who has not had a skill entered for them. This table has a serial of the user column aswell which is unique. If there is more information that i can give you to assist me, please ask me. You help would be greatly appreciated.
Hi there, I want to create an SQL Function that checks if a table exists and returns true or false. I will pass this function a paramter (say @COMPANYID) e.g.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblmyTableName_' + @COMPANYID) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
how would I write this so the query is dynamically executed and I can get a value of true or false back?
I want to know through applicaiton how to check whether the Server named"Myserver" is Exists with Database named "MyDatabase" with Tables named ('Table1','Table2','Table3').
For any Database like (MS Access, Oracle, MS SQL-Server,MySQL)..
I am writing my first SSIS task. I have an MS Access database and I'm moving the contents of each table into a like-named SQL Server database. The task is working properly except for one step.
The source Access database may or may not have one of the tables in it. It will always have TableA, TableB, and TableC, but may or may not have TableD. I need to write my SSIS package in such a way that it will detect the presence/absence of TableD and either run or skip the import step for this table accordingly.
Can someone assist with the step that detects the table presence/absence in the MSAccess database?
I'm SQL Server 2005 newbie, could you be so kind and help me how to write the most efficient way stored procedure, which checks by PK if specified row exists, then updating the row, otherwise inserting new one.
Hello,I created the following SQL script to check if a record exists:IF (EXISTS (SELECT LevelName FROM dbo.by27_Levels WHERE LOWER(@LevelName) = LOWER(LevelName))) Return (1)ELSE Return (0)And I also found in a web page another solution:IF EXISTS(SELECT 1 FROM TABLENAME WHERE LevelName=@LevelName) SELECT 1ELSE SELECT 0- Which approach should I use?- Why "SELECT 1 FROM"?- And when should I use SELECT or RETURN?All I need is to know if the record exists ... nothing else.I will use this procedure on an ASP.NET 2.0 / C# web site.I am not sure if this important but anyway ...Thank You,Miguel
Hi, I was wondering if someone can help. In vb.net what is the best way to check if a record exists if you are using an sql data reader? For my application I need to display a button control (make it visible on the page) if a record is available after executing my sql select statement. cheers Mark :)
Does anybody can tell me how to check if cursor exists ? I have a "try and catch" blocks. I want to destroy the cursor when error occurred and it jumps into "catch" block. Before I destroy the cursor I want to check if it exists, because the error could occurred before I have declared or opened the cursor.
I have created a formview which I among other things uses to insert new values into a database. What I want to check is if the primary key which is put into the form already exists in the db. If it is I want to get a message to my web page, if not the data can be inserted.
How can I do this?
And if the only way to control this is to create a stored procedure. How do I write such a proc?
I declare a cusror named crInv inside a loop. Since I open this cursor a lot of times I want to check if is already opened. I try to use the Cursor_status function but it always returns -3. The syntax is:
DECLARE crInv SCROL CURSOR FOR SELECT Val1, Val2 FROM TABLE1 WHERE Val3=450
If Cursor_Status('local','crInv')>0 BEGIN CLOSE crInv DEALLOCATE crInv END
This code is inside a loop. If I PRINT Cursor_Status('local','crInv') before and after the DECLARE statement it always returns -3. What is wrong??
Is there a way in T-SQL to check to see if a #tempTable exists? I want to write a proc the uses a temp table, but I first need to see if the table already exists. if it does I want to drop it, otherwise skip
In SSIS, I need an easy way to see if a file exists, and if not wait for it until a timeout period expires. Here are the options I've discovered, along with the issues I've had:
a) The File Watcher task from www.sqlis.com
This was my first attempt. The task works great, BUT only detects when there is a change on the file. If the file already exists, it keeps waiting which is not the behavior I need. b) The WMI Event Task
There is very sparce documentation on this event and how to write a WQL query. There are numerous examples of monitoring a folder and if any files appear, cause an event to happen. I need to detect for a specific file. I found maybe one example of this using "PartComponent" but wasn't able to get the sytax right to make it work for me. I also need to access a remote file share using a UNC path (e.g. \servernamepathfile.txt) which I could not get to work. c) Script Task using the File.Exists() method
I imported the System.IO namespace, and used a File.Exists(\servernamepathfile.txt) with actual success, but am not sure of the best way to continue to wait if the file is not found immediately. I also want to modularize this approach so I can wait for several files simultaneously so was thinking of implementing this script task as a package by itself to accept variables (filepath & timeout period) but need to know if anyone has had success with this approach. I'm open to suggestions or ways to get options a) and b) to work for my needs. Thanks! Kory
Well, actually, as in the title. I have a table. The script should add a column if that column doesn't exist already. I use VB to combine the two queries. So what I want:
I was able to get this code to work but now I get a SQL error if you try to submit the same information twice. How can I add a message saying that the "email" already exists in database without the SQL error? protected void Button1_Click1(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=TECATE;Initial Catalog=subscribe_mainSQL; User Id=maindb Password=123456; Integrated Security=SSPI"); SqlCommand cmd = new SqlCommand("INSERT INTO [main] (, [userid], [fname], [lname], [degree]) VALUES (@email, @userid, @fname, @lname, @degree)", conn); conn.Open(); cmd.Parameters.AddWithValue("@email", email.Text); cmd.Parameters.AddWithValue("@userid", uscid.Text); cmd.Parameters.AddWithValue("@fname", fname.Text); cmd.Parameters.AddWithValue("@lname", lname.Text); cmd.Parameters.AddWithValue("@degree", degree.SelectedItem.Value); int i = cmd.ExecuteNonQuery(); conn.Dispose(); }
OK i have script which is to be run on several databases. Within this script there are commands to create a primary key on a specific table. Can anyone tell me if it is possible to check if a specific primary key exists on a table?
What is the best way to check for an existing table in a db (i.e. check to be performed when creating a table) I have came accross the following:
Code Block
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='someTable')
and
Code Block if not exists (select * from dbo.sysobjects where id = object_id('[someUser].[someTable]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
Note I want to work with tables that can have any name (unicode chars accepted);
I realise that INFORMATION_SCHEMA.TABLES is a view, and views are generally preffered, but why then does enterprise manager create the SQL found in the second example?
If anyone could explain what is best practice, it would be much appreciated,