Checking If Directory Exists In T-SQL

Jun 18, 2008

I know there is a way to determine if a file exists using T-SQL, but I can't seem to find a way to determine if a directory exists. I need to be able to determine this so I can delete the directory if it already exists before I run other queries.

View 17 Replies


ADVERTISEMENT

Checking If File Exists Or Not

Mar 18, 2008

Greetings all.

How can I check for file exitence in a specified folder using TSQL?

I can think of a couple of ways to do this so any feedback would be appreciated.

Load the contents of a folder in to a temporay table and check the table for file name. I can do this with something like:

insert into myFiles
exec master..xp_cmdshell 'dir /B /OND filePath*.txt'


Or maybe use OLE Automation Sproc? But I am not sure which method I should use.

Please advise.

View 4 Replies View Related

Checking To See If A Record Exists Before Inserting

Feb 14, 2007

I can't seem to get this work.  I'm using SQL2005
I want to check if a record exists before entering it.  I just can't figure out how to check it before hand.
Thanks in advance. Protected Sub BTNCreateProdIDandName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTNCreateProdIDandName.Click
' Define data objects
Dim conn As SqlConnection
Dim comm As SqlCommand

' Reads the connection string from Web.config
Dim connectionString As String = ConfigurationManager.ConnectionStrings("HbAdminMaintenance").ConnectionString
' Initialize connection
conn = New SqlConnection(connectionString)

' Check to see if the record exists
If
comm = New SqlCommand("EXISTS (SELECT (BuilderID, OptionNO FROM optionlist WHERE (BuilderID = @BuilderID) AND (OptionNO = @OptionNO)", conn)

Then
'if the record is exists display this message.
LBerror.Text = "This item already exists in your Option List."
Else


'If the record does not exist, add it. FYI - This part works fine by itself.
comm = New SqlCommand("INSERT INTO [OptionList] ([BuilderID], [OptionNO], [OptionName]) VALUES (@BuilderID, @OptionNO, @OptionName)", conn)

comm.Parameters.Add("@BuilderID", System.Data.SqlDbType.Int)
comm.Parameters("@BuilderID").Value = LBBuilderID.Text

comm.Parameters.Add("@OptionNO", System.Data.SqlDbType.NVarChar)
comm.Parameters("@OptionNO").Value = DDLProdID.SelectedItem.Value

comm.Parameters.Add("@OptionName", System.Data.SqlDbType.NVarChar)
comm.Parameters("@OptionName").Value = DDLProdname.SelectedItem.Value

LBerror.Text = DDLProdname.SelectedItem.Value & " was added to your Option List."

Try
'open connection
conn.Open()
'execute
comm.ExecuteNonQuery()
Catch
'Display error message
LBerror.Text = "There was an error adding this Option. Please try again."
Finally
'close connection
conn.Close()
End Try

End If

End Sub  
 

View 8 Replies View Related

Checking If An Entry Exists In A Database

Apr 22, 2006

I would like to be able to check if a certain entry exists in a SQL table. Ideally, the output would be a boolean so I can use it in an IF statement which would tell it what to do depending on whether or not the entry exists. Thanks for anyone that helps. :)

View 6 Replies View Related

Checking A Table To See If A Record Already Exists

Sep 19, 2007

I've got two tables, one containing a list of company names(approx 10,000 records), the other containing a list of company employees (approx 30,000 records) joined by the CompanyID column.

I have a third table (approx 700 records) containing new employees to be added to the employee table. Each record in this table has the employees details plus the name of their company.

I want to write a query that will check each row in the third table to see if
a) the employee exists in the Employees table
b) the company exists in the Companies table and
c) the employee is listed under the correct company

The query should also handle any combination of the above. So if the company doesn't exist but the employee does, create the company on the companies table and update the appropriate record on the employees table with the new CompanyID etc. etc.

Oh, forgot to mention. The company names in the third table won't be exactly the same as the ones in the Company table so will need to use CharIndex.

Anybody got any ideas?

View 4 Replies View Related

Checking Whether Record ID Exists In Another Query

Jan 10, 2014

I'm trying to check which price grids are in use using the price grid_id, and seeing whether this grid_id exists in another query that checks all active contracts. If the grid_id is present (active) I want to return 'Yes', if not I want it to return 'No'.

There are 385 price grids, but my query is only returning the 315 that are active, and ignoring any that are not used. My code is below, how I can see all the records whether Yes or No:

Select distinct
pg.grid_id [Price Grid],
pg.grid_name [Grid Name],
case when exists
(Select
c.grid_id from
customers c
inner join deltickhdr dh on dh.acct = c.custnum and dh.stage <5
where
c.type = 'C' and
dh.dticket is not null) then 'Yes' else 'No' end [Active]

From gridhdr pg inner join customers c on c.grid_id = pg.grid_id

Order by pg.grid_id

View 4 Replies View Related

Checking If Information Exists Before Copy

Apr 1, 2007

I am getting an error between the IF THEN Else statement. The copy works after the THEN! I need some help!

Ernie

I have two tables with script in a view. 1. newusers 2. blacklist I want to compare that "PresentEmail, RemoteComputer,Email" thats in newuser table is not in blacklists, and if this is true, then copy from newuser (the record not on blacklist) to blacklist using a where statement for second condition.

IF
(NOT EXISTS
(SELECT *
FROM newusers AS d
WHERE ISNULL(PresentEmail, 'NULL') IS BadEmail))


THEN

Insert into blacklist (BadNewEmail, BadIpAddress, BadEmail)

Select NewEmail, RemoteComputerName, PresentEmail

From newusers

Where Account_Active = 'Spammer'

Else
GO

View 7 Replies View Related

Checking To See If A Record Exists Else Insert

Apr 30, 2008

I am checking to see if the source record is available in the target table using a lookup transformation and if not found i have to insert this record.
I have connected the error flow of the lookup transformation to the target. I am acheiving expected results, but is this the best practise? I have not connected to the green arrow to any task.

View 6 Replies View Related

T-SQL (SS2K8) :: Checking User Active Directory Group

Jul 17, 2014

Is it possible to check for Active Directory group.. ie see if the user running the Stored Proc, is in a specific Active Directory Group? Or if I set up Login's using Active Directory, can I get the Login that way... or will it give me the user's account?

View 6 Replies View Related

Checking If A Stored Procedure Exists Before Creating It

Oct 27, 2005

I'm trying to put scripts to create our stored procedures under version control.

However I don't want these scripts to be run if the stored procedure already exists (we'll be using update scripts to alter existing stored procedure I think).

Anyway I tried :


Code:


SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

/****** Object: Stored Procedure dbo.getEnglandHotelsByATOPResort Script Date: 26/10/2005 10:40:01 ******/
if NOT EXISTS (SELECT object_id('procedureName','p'))
CREATE PROCEDURE [dbo].[procedureName]
@location char(2)
AS
...Procedure...

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



I know that the if NOT EXISTS ... part works as doing the following returns the expected result :


Code:


IF NOT EXISTS (SELECT object_id('procedureName','p'))
SELECT 'FALSE'
ELSE
SELECT 'TRUE'



However when trying to do this before the CREATE PROCEDURE I get :

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'PROCEDURE'.
Server: Msg 137, Level 15, State 1, Line 12
Must declare the variable '@location'.

Any ideas how to do this?

-D

View 3 Replies View Related

SQL 2012 :: Checking If A Date Exists In Particular Daterange

Jan 6, 2015

I have a table called as Events and below are its columns

ID int
EventFromDate datetime
EventToDate datetime
EventDesc nvarchar
IsHoliday bit

This is a master table where the admin would enter the Events/Holidays for the entire year.

The data would be as below:

IDEventFromdateEventTodateEventDesc isHoliday
126-01-201526-01-2015RepublicdayYes
230-01-201531-01-2015TeamOutingNo
301-05-201501-05-2015Labour day Yes

Now, suppose a employee applies leave on 26/01/2015 to 26/01/2015 then it should not insert into table and return a value "Not updated"

How to handle the scenario if a employee applies leave between the range 23/01/2015 to 27/01/2015, since 26/01/2015 is a holiday in between. how the data can still be inserted excluding 26/01/2015

Can we exclude a non-working day or a sunday.

Leavedetails table to insert leaves applied by employee is as follows

LeaveDetailID int
LeaveTypeId int
FromDate datetime
EndDate datetime
Remarks nvarchar

View 0 Replies View Related

Checking To See If A Record Exists And If So Update Else Insert

Feb 9, 2007

I've decided to post this as a sticky given the frequency this question is asked.

For those of you wishing to build a package that determines if a source row exists in the destination and if so update it else insert it, this link is for you.

http://blogs.conchango.com/jamiethomson/archive/2006/09/12/SSIS_3A00_-Checking-if-a-row-exists-and-if-it-does_2C00_-has-it-changed.aspx

Thanks Jamie!

If you want to do a similar concept to Jamie's blog post above, but with the Konesan's Checksum Transformation to quickly compare MANY fields, you can visit here:
http://www.ssistalk.com/2007/03/09/ssis-using-a-checksum-to-determine-if-a-row-has-changed/

Phil

View 60 Replies View Related

Checking To See If A Records Exists Before Inserting - 3 Million + Rows

Aug 21, 2007

I have 1+ CSV files (using a foreach loop) which I'm doing a lot of transform work on and then inserting into a SQL database table.
Each CSV file usually contains about 2 days worth of data (contains date stamps) - somewhere in the region of 60k records per day.
The destination table currently contains 3 million+ rows and will get bigger.
I need to make sure that before inserting into the destination table, the data doesn't already exist.

I've read the following article: http://www.sqlis.com/311.aspx
While the lookup method works, it takes ages and eats up memory as it caches the 3m+ records before running for each CSV. Obviously this will only get worse as the table grows in size.

To make things a little more efficient what I'd like to do, is first derive the dates I'm dealing with in the current file - essentially storing the max(date) and min(date) in variables. Then in the lookup SQL use those vars, to reduce the amount of data that needs to be brought into the transformation to check against before inserting into the destination table.
Lookup SQL eg. SELECT * FROM MyTable WHERE Date BETWEEN varMinDate AND varMaxDate.

Ideally I'd use an aggregate transformation and then use the subsequent output from that either in the lookup query or store the output in vars, but I don't think you can do that and I get the feeling I'm approaching this with the wrong mindset.

Any thoughts would be great!

View 6 Replies View Related

Checking Specific Columns Were NULL On Previous Insert (Using If EXISTS)

Aug 3, 2007

Thanks for your time:

Background: After Insert Trigger runs a sproc that inserts values into another table IF items on the form were
populated. THose all work great, but last scenario wont work: Creating a row insert based on Checking that all 22 other items from the prior insert (values of i.columns) were NULL:

IF EXISTS(select DISTINCT i.notes, i.M_Prior, i.M_Worksheet, ...
from inserted i
WHERE i.notes IS NOT NULL AND i.M_Prior = NULL AND i.M_Worksheet = NULL AND...)


BEGIN

Insert into dbo.Items2Fix ...

From inserted i

END

View 1 Replies View Related

Using Provider For Microsoft Directory Services For Querying Active Directory

Apr 6, 2007

Has anyone used this successfully from an OLEDB source component, or even from the Execute SQL Task? I've seen some examples of using a script component, but nothing that uses it through a connection manager.

View 6 Replies View Related

IF NOT EXISTS (... - EXISTS TABLE : Nested Iteration. Table Scan.Forward Scan.

Sep 20, 2006

Hi,

This is on Sybase but I'm guessing that the same situation would happen on SQL Server. (Please confirm if you know).

I'm looking at these new databases and I'm seeing code similar to this all over the place:

if not exists (select 1 from dbo.t1 where f1 = @p1)
begin
select @errno = @errno | 1
end

There's a unique clustered in dex on t1.f1.

The execution plan shows this for this statement:

FROM TABLE
dbo.t1
EXISTS TABLE : nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.

It's not using my index!!!!!

It seems to be the case with EXISTS statements. Can anybody confirm?

I also hinted to use the index but it still didn't use it.

If the existence check really doesn't use the index, what's a good code alternative to this check?

I did this and it's working great but I wonder if there's a better alternative. I don't really like doing the SET ROWCOUNT 1 and then SET ROWCOUNT 0 thing. SELECT TOP 1 won't work on Sybase, :-(.

SET ROWCOUNT 1
SELECT @cnt = (SELECT 1 FROM dbo.t1 (index ix01)
WHERE f1 = @p1
)
SET ROWCOUNT 0

Appreciate your help.

View 3 Replies View Related

Checking For PK

Feb 10, 2004

Hi,

I want to assign a list columns to be the PK on a table. I don't know what this list of columns would be.How do I go about figuring that?

Sample Data

Col1...Col2...Col3..Col4
1...........2.......A......X
1...........2........B.....X
1...........2.......A......Y



As you can see Col1 & Col2 ONLY do not make up the PK. I need to include Col3 as well. My concern is I am not sure if I have a row like..
1...........2.......A......X reapeated lower down the order. So how do I figure the PK out?

Thanks

View 2 Replies View Related

Checking For Row Existence

Nov 1, 2006

Can someone show me some C# code for detecting if a SQL row exists or not?  This seems like a very typical action and I cannot for the life of me find a tutorial online that explains this step.  In my code I'm either going to INSERT or UPDATE a record.  I tried sending a SELECT command through a ExecuteNonQuery, but only got -1 as a response.  Apparently ExecuteNonQuery does not work with SELECT.  I then saw that T-SQL has an EXISTS keyword, but I cannot see anyway to use that from within C#.So...can anyone share the typical code they use to identify if a row exists or not within a database.  I guess I was execting there to be some method available to do this sort of thing.

View 1 Replies View Related

Checking Value In 2 Tables.

Aug 3, 2007

i want to check a value in 2 tables. the 1st table i want to check if the value exists and from the 2ed table i samething.
i came up with this but when it's doing the second if i get error on the page.
Dim ReturnVal As Integer
Dim ReturnVal2 As IntegerDim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("imacstestConnectionString").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand("SELECT [ReportNumber] FROM [AppraisalSummaryBlue] WHERE ([ReportNumber] = @ReportNumber)", conn)
'To check if the # is registered.Dim cmd2 As SqlCommand = New SqlCommand("SELECT [ReportNumber] FROM [t_RegisterInfoTemp] WHERE ([ReportNumber] = @ReportNumber)", conn)
cmd.Parameters.AddWithValue("@ReportNumber", txtReport.Text)cmd2.Parameters.AddWithValue("@ReportNumber", txtReport.Text)
conn.Open()
ReturnVal = Convert.ToInt32(cmd.ExecuteScalar())
conn.Close()
'This checks if the # is correct.
If ReturnVal > 0 Then
'This checks if its registered with someone else.
conn.Open()
ReturnVal2 = Convert.ToInt32(cmd2.ExecuteScalar())
conn.Close()
If ReturnVal2 <> 0 ThenServer.Transfer("regccinfo.aspx")
Else
lblError.Text = "the # is registered with someone else."
End If
Else
lblError.Text = "the # does not exists."
txtReport.Focus()
End If

View 6 Replies View Related

Checking For A New Record

Jan 5, 2005

Good morning everyone I am writing a windows forms application that will work similar to the windows messenger popup. I need this application to display a message to the user whenever a record is added to table in my DB. Any help would be appreciated in explaining how I can check the DB for new records.

View 3 Replies View Related

Checking Character Set

May 2, 2002

I've forgotten the character set that I've chosen when I was installing the SQL Server 7. Is there a way to check?

I'm currently using US English version of Windows NT4 and SQL7. But interestingly, all the data is in Japanes characters. It's actually for a Japanese website, and the front-end application is written in ASP. I remember reading somewhere that it is impossible to do certain type of sorting (by some particular order for the Japanese language) as it is limited by the choice of the language of the NT OS.

The type of sorting that I'm looking at is the grouping of 5-characters. One example is in http://www.forest.impress.co.jp/aiueo.html

Would this be possible with my current setup? Or would it help if I migrate over to Windows 2000? (I'd rather not move to Japanese NT4)

Your feedback and advice would be very much appreciated!

View 1 Replies View Related

Checking @@Error

Feb 20, 2003

Hi,

Just a brief question. I have a script which does a number of insert statements. What I would like to do is determine if the insert statements were all successful. Aside from checking @@ERROR after every insert, is there a way to check if all the insert statements completed successfully?

Thanks,

Jim

View 2 Replies View Related

Checking For A Range

Aug 5, 2004

I want to do a check to see if a number falls within a range specified by the user. Basically, they enter in a min and max value and I return a hit if a number falls inbetween.

I have having trouble with my SQL statement:


Code:

SELECT DISTINCT *
FROM Table
WHERE ( (0=0)
AND
(min.value <= Table.Value <= max.value) )


(This isn't my actual code, but just the logic)

It will return all the results everytime. Does anyone have any suggestions on what I could be missing here?

Thanks in advance!

View 2 Replies View Related

Checking Job Status Using SQL-DMO

Jun 27, 2004

I have a question I hope someone can help me with.

My situation:
I have a single-step job in SQL Server, which runs a stored procedure, A. This stored procedure invokes another stored procedure, B. In B, one of the statements is a 'BACKUP DATABASE' command, and a database is backed up to a file.

The job is started by an application. Once started, the application then uses the SQL-DMO property CurrentRunStatus to periodically check the status of the job. When the property returns the value SQLDMOJobExecution_Idle (indicating the job has completed), the application code then continues processing, and attempts to access the .dat file produced by the 'BACKUP DATABASE' command.

My problem:
On occasion, the application will hit the problem where either the backup file cannot be located, or the file is still being locked by another process (Error=The process cannot access the file because it is being used by another process).

Is anyone able to shed some light on this?

I assume that the job will only return a completed status after:
(a) both A and B have completed execution, and
(b) the BACKUP operation has completed

Is it possible that even though SQL Server indicates the job has finished, that the BACKUP operation still hasn't completely ended?

Thanks for any help,
Andrew

View 2 Replies View Related

Checking Agents

Oct 8, 2007

I have around 25 sql servers(sql server 2000) all on windows server 2003. i would like to know if anyone has a script that will poll all the servers and check to make sure the agent is running.

thanks for your help

View 2 Replies View Related

Table Checking

Jan 9, 2004

I need to check one table while updating another table
suppose i need to check table1's value is whether less then 0 or not
without if condition can i check it with update query?
thank you

View 5 Replies View Related

Backup Checking

Feb 25, 2004

Hello, everyone:

After backup is finished, how to check if the backup is successful, and it is same as the original database? Thanks.

ZYT

View 1 Replies View Related

Checking For Null

May 23, 2008

I have a table that I know has a null or nothing in a field. When I run this query to get a list of them nothing comes back at all but it doesn't error out. Any ideas?

select *
from lab_test_add_conf
where table_name = null
order by lab_test_conf_id

View 5 Replies View Related

Version Checking

Mar 18, 2007

I chaps and chappets.

It's been a while!

This is going to sound ignorant but...
I had a look here http://support.microsoft.com/?id=321185

And one thing isn't clear. How doe you know if the 32-bit or 64-bit version was installed?

------------------------
Me: What do you want to know from your data warehouse?
Client: Err...Emm...Everything
Me: OK, that's great. That's all I need to know. I'll see you when it's done.

View 1 Replies View Related

Checking For Availability

May 22, 2007

i'm not fluent with sql and i have a question to ask..

if i want to check whether a product is available between two dates, how can i do so? for example, i want to check whether it's available between 5/23/2007 to 5/25/2007..

thanks

View 11 Replies View Related

Way Of Checking To See If All SP's Work

Jul 25, 2007

Our System currently has on the upwards of 300 stored procedures in it.

We have SP's that call SP's that call SP's and sometimes a Stored Procedure will return 10 ResultSets which are used by .NET.

Some of our developers do not check other stored procedures when they modify the SP parameters, and in doing so break the system until the testers find it.

is there a way to run a command to check ALL the stored procedures to see if they work, they match the SP they're calling, etc?

View 20 Replies View Related

Data Checking?

Jul 23, 2005

Hey all,prolly a simple solution, but why isn't the following string working inmy execute sql step within DTS? It produces results, just not the onesI want... What am I doing wrong?select x from new_files where x like '%[^0-9]%' and x like '%[^a-z]%'It's displaying all the records? It should only be displaying thoserecords that do *not* contain letters or numbers.Thanks in advance!-Roy

View 3 Replies View Related

Best Way To Add To DB While Checking For Duplicates

Jul 23, 2005

Situation:Day 1Table contains 100 items of actions imported via FTP. One of the fieldsin the table can be updated to reflect an assigned unit code.Day 2Actions that may be duplicates of the table from Day 1 (with theexception of the updated assigned unit field) are imported forinclusion in the Table from Day 1.Question- What is the best way to insure that any new items are addedto the table and that no duplicates are added at the sametime?Assistance would be appreciated.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved