Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop Check of SELECT Clause

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
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:
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
 
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
 
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:
	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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top