INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Stop Check of SELECT Clause

Stop Check of SELECT Clause

(OP)
I am loading the contents of an EXCEL workbooks into SQL database tables. Some of these workbooks have worksheets called EquipmentWarranty and Warranty, and some have just Warranty. I would like to avoid having to have the user flip back an forth between two different scripts, but every time I run the code that contains code for both within IF statements, it stops when it checks the section that deals with workbooks having both worksheets and declares an error. I have also tried this with separate IF statements as well as labels and GOTO statements. Nothing seems to work.

CODE --> T-SQL

DECLARE @EquipWarrantyPresent		BIT	
SET @EquipWarrantyPresent = 0           -- 1 = EquipmentWarranty exists - 0 = EquipmentWarranty does not exist

IF @EquipWarrantyPresent = 1
BEGIN
	BEGIN TRY
	INSERT INTO dbo.Warranty
	(
		fldEquipmentTagID,
		fldEquipmentType,
		fldManufacturer,
		fldModel,
		fldWarrantyNumber,
		fldStartDate,
		fldEndDate,
		fldRenewalOptions,
		fldcompany,
		fldcontactname,
		fldAddressStreet,
		fldAddressCity,
		fldAddressStateProvince,
		fldAddressCountry,
		fldContactPhone,
		fldContactEMail,
		fldRemarks
	)
	SELECT 
		ew.EquipmentTagID,
		ew.EquipmentType,
		ew.Manufacturer,
		ew.Model,
		ew.WarrantyNumber,
		w.StartDate,
		w.EndDate,
		w.RenewalOptions,
		w.Company,
		w.ContactName,
		w.AddressStreet,
		w.AddressCity,
		w.AddressStateProvince,
		w.AddressCountry,
		w.ContactPhone,
		w.ContactEMail,
		w.remarks
	FROM CMMSSrc...[EquipmentWarranty$]ew
	INNER JOIN CMMSSrc...[Warranty$] w
	ON ew.fldWarrantyNumber = w.fldWarrantyNumber
	END TRY
	BEGIN CATCH
		SELECT
			ERROR_NUMBER() AS ErrorNumber,
			ERROR_SEVERITY() AS ErrorSeverity,
			ERROR_STATE() AS ErrorState,
			ERROR_PROCEDURE() AS ErrorProcedure,
			ERROR_LINE() AS ErrorLine,
			ERROR_MESSAGE() AS ErrorMessage;
		IF ERROR_NUMBER() = 7399
		BEGIN
			SELECT 'Check file name CAREFULLY! Check underscores!'
		END
	END CATCH
END
ELSE
BEGIN
	BEGIN TRY
	INSERT INTO dbo.Warranty
	SELECT *
	FROM CMMSSrc...[Warranty$]
	WHERE [Equipment Tag ID] is NOT NULL
	END TRY
	BEGIN CATCH
		SELECT
			ERROR_NUMBER() AS ErrorNumber,
			ERROR_SEVERITY() AS ErrorSeverity,
			ERROR_STATE() AS ErrorState,
			ERROR_PROCEDURE() AS ErrorProcedure,
			ERROR_LINE() AS ErrorLine,
			ERROR_MESSAGE() AS ErrorMessage;
		IF ERROR_NUMBER() = 7399
		BEGIN
			SELECT 'Check file name CAREFULLY! Check underscores!'
		END
	END CATCH
END 

RE: Stop Check of SELECT Clause

Could it be as simple a fix as this?

CODE

SET @EquipWarrantyPresent = CASE WHEN OBJECT_ID('CMMSSrc...[EquipmentWarranty$]', 'U') IS NOT NULL THEN 1 ELSE 0 END 

RE: Stop Check of SELECT Clause

(OP)
Thank you. Unfortunately, it appears the the OBJECT_ID() function only works within the existing server. When run as above, it always came up NULL (ie. 0). Perhaps it could be made to work when the link is to another SQL Server, but the only linked servers we have here are EXCEL workbooks.

After following various links from Books on Line > OBJECT_ID(), I finally found my way to Link. I modified the code at the bottom of the page to

CODE --> T-SQL

DECLARE @tables			TABLE
	(
		TABLE_CAT nvarchar(100),
		TABLE_SCHEM nvarchar(100),
		TABLE_NAME nvarchar(100),
		TABLE_TYPE nvarchar(100),
		REMARKS nvarchar(100)
	)

INSERT @tables EXEC sp_tables_ex @table_server = 'CMMSSrc'

IF EXISTS
(
	SELECT * FROM @tables
	WHERE TABLE_NAME = 'EquipmentWarranty$'
)
BEGIN
	SET @MainEquipTypePresent = 1
END
ELSE
BEGIN
	SET @MainEquipTypePresent = 0
END 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close