I am building a recipe manager, and I am not sure this is the best way to do it, as far as data goes.
I've got a named query that takes a string argument, and query calls a stored procedure in SQL Server that returns a dataset based on the passed string.
The sproc takes the first four letters of the string, because the first four are standardized.
Here is an example list of recipes:
list = ['A111-100','A111-150','B211-100','B211-100','B211X-150']
The last one has an extra designator, and the three following digits represent the speed of the machine. So as we are able to increase the speed, the recipe must change to get the same specs on the product.
What I have created so far are a bunch of SQL views, that are named with everything but the speed, so if I were to look at the dataset for A111
, I would get both the 100 and 150 speed recipes to look at. I didn't want to create a view for every possible speed we might run at, which could double or triple the number of SQL views.
This is my sproc, which selects which view to grab the data from based on the string passed from the named query:
CREATE PROCEDURE [dbo].[usp_SelectRecipeView]
-- Add the parameters for the stored procedure here
@recipe VARCHAR(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @View nvarchar(30)
DECLARE @Sql nvarchar(4000)
DECLARE @recipe_Short nvarchar(7)
--SET @Sql = N'SELECT * FROM Recipe.'
SET @View = 'dbo.'
SET @recipe_Short = (SELECT LEFT(@recipe, CHARINDEX('-', @recipe) - 1))
-- Insert statements for procedure here
SET @View = @View + (SELECT v.name
FROM sys.views AS v
WHERE v.name LIKE '%' + @recipe_Short + '%')
--SELECT @View
SET @Sql = N'SELECT * FROM ' + @View
--SELECT @Sql
--EXECUTE sp_executesql @Sql, N'@View nvarchar(30)'
EXEC (@Sql)
END
GO
I have not determined how to prevent more than one dataset being returned at the moment. For instance, if I pass B211
, I would get B211
and B211X
datasets.
For this issue, my current thought is creating an SQL view for each recipe...OR, perhaps I could check for an additional character (like X
) just before the dash -
...
My questions are:
- Should I create an SQL view for each individual recipe?
- How can I catch an empty dataset so the user does not get the error dialog box? Some recipes do not have data yet...
Thanks.