Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2004-04-13 : 16:39:21
|
I have a stored procedure that will return the following string:(108 * 4) + (((36 - 4) * (5)) * 2) Are there any already written functions/stored procedures that can parse and compute the above string?If not, can someone help me get started on this?Mike B |
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2004-04-13 : 16:42:27
|
Let me guess. This is produced dynamically. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-13 : 16:44:16
|
Does this not work:create proc test(@var1 varchar(500) output)asset @var1 = (108 * 4) + (((36 - 4) * (5)) * 2)returndeclare @int intexec test @int outputprint @intdrop proc testSo when you execute it, it does the computation since you are using INT.Tara |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2004-04-13 : 17:30:58
|
I can get your example to work, but not the real thing. It gives the error : "Error converting data type varchar to float." USE NORTHWINDGOcreate proc test@input varchar(500) output,@var1 varchar(500) outputasset @var1 = @inputreturnGOdeclare @int int, @Input varchar(500)set @Input = '(108 * 4) + (((36 - 4) * (5)) * 2)'exec test @Input, @int outputprint @intGOdrop proc testGO The expression is created dynamically using:SELECT @cResult AS FormulaDECLARE AttributeCursor CURSOR FOR SELECT sa.Attribute, av.AttributeValue FROM tbProductTemplateAttributeValues AS av INNER JOIN tbShapeAttributes AS sa ON av.fkAttributeID = sa.AttributeID WHERE av.fkTemplateID = @iTemplateOPEN AttributeCursorFETCH NEXT FROM AttributeCursor INTO @cAttribute, @fAttributewhile(@@FETCH_STATUS = 0) BEGIN SELECT @cResult = REPLACE(@cResult, @cAttribute, CAST(@fAttribute AS VarChar)) FETCH NEXT FROM AttributeCursor INTO @cAttribute, @fAttribute ENDCLOSE AttributeCursorDEALLOCATE AttributeCursorGO The SELECT in red above is how the expression is created.Mike B |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2004-04-13 : 19:25:03
|
You can use the following DDL and DML to see exactly what I am trying to do:USE NORTHWIND GOSET NOCOUNT ONCREATE TABLE [dbo].[tbProductCodes] ( [ProductCode] [int] NOT NULL , [fkAccountID] [int] NOT NULL , [Product] [varchar] (50) NOT NULL , [fkShapeID] [int] NOT NULL ) ON [PRIMARY]GOINSERT INTO tbProductCodes (ProductCode, fkAccountID, Product, fkShapeID)SELECT 2001, 1, 'New Product', 1GOCREATE TABLE [dbo].[tbProductTemplateAttributeValues] ( [fkTemplateID] [int] NOT NULL , [fkAttributeID] [int] NOT NULL , [AttributeValue] [float] NOT NULL ) ON [PRIMARY]GOINSERT INTO tbProductTemplateAttributeValues (fkTemplateID, fkAttributeID, AttributeValue)SELECT 1, 1, 108 UNION ALLSELECT 1, 2, 36 UNION ALLSELECT 1, 3, 4 UNION ALLSELECT 1, 4, 5 UNION ALLSELECT 1, 5, 2 GOCREATE TABLE [dbo].[tbProductTemplates] ( [TemplateID] [int] NOT NULL , [fkProductCode] [int] NOT NULL , [Template] [varchar] (50) NOT NULL , [fkMixID] [int] NULL ) ON [PRIMARY]GOINSERT INTO tbProductTemplates (TemplateID, fkProductCode, Template, fkMixID)SELECT 1, 2001, 'ProductTemplate', 1GOCREATE TABLE [dbo].[tbShapeAttributes] ( [AttributeID] [int] NOT NULL , [fkShapeID] [int] NOT NULL , [Attribute] [varchar] (50) NOT NULL ) ON [PRIMARY]GOINSERT tbShapeAttributes (AttributeID, fkShapeID, Attribute)SELECT 1, 1, 'Width' UNION ALLSELECT 2, 1, 'Height' UNION ALLSELECT 3, 1, 'Flange' UNION ALLSELECT 4, 1, 'Leg' UNION ALLSELECT 5, 1, 'Count' GOCREATE TABLE [dbo].[tbShapes] ( [ShapeID] [int] NOT NULL , [Shape] [varchar] (50) NOT NULL , [Formula] [varchar] (100) NULL ) ON [PRIMARY]GOINSERT INTO tbShapes (ShapeID, Shape, Formula)SELECT 1, 'Double T', '(Width * Flange) + (((Height - Flange) * Leg) * Count)'GO CREATE PROCEDURE usp_shapes_GetCrossSection@iTemplate int,@cResult varchar (500) OUTPUTASdeclare @cAttribute varchar(50), @fAttribute float-- Get the formula for the templates shapeSELECT @cResult = s.Formula FROM tbShapes AS s INNER JOIN tbProductCodes AS pcON s.ShapeID = pc.fkShapeIDINNER JOIN tbProductTemplates AS ptON pc.ProductCode = pt.fkProductCodeWHERE pt.TemplateID = @iTemplateSELECT @cResult AS FormulaDECLARE AttributeCursor CURSOR FOR SELECT sa.Attribute, av.AttributeValue FROM tbProductTemplateAttributeValues AS av INNER JOIN tbShapeAttributes AS sa ON av.fkAttributeID = sa.AttributeID WHERE av.fkTemplateID = @iTemplateOPEN AttributeCursorFETCH NEXT FROM AttributeCursor INTO @cAttribute, @fAttributewhile(@@FETCH_STATUS = 0) BEGIN SELECT @cResult = REPLACE(@cResult, @cAttribute, CAST(@fAttribute AS VarChar)) FETCH NEXT FROM AttributeCursor INTO @cAttribute, @fAttribute ENDSELECT @cResult AS NewFormulaCLOSE AttributeCursorDEALLOCATE AttributeCursorGO-- Test stored procdeclare @iTemplate int, @fResult floatSET @iTemplate = 1EXECUTE usp_shapes_GetCrossSection @iTemplate, @fResult OUTPUTSELECT @fResult AS ResultGOdrop table [dbo].[tbProductCodes]GOdrop table [dbo].[tbProductTemplateAttributeValues]GOdrop table [dbo].[tbProductTemplates]GOdrop table [dbo].[tbShapeAttributes]GOdrop table [dbo].[tbShapes]GODROP PROCEDURE usp_shapes_GetCrossSectionGO Mike B |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-13 : 19:41:52
|
I can't get it to work either. Why can't you compute the value inside the stored proc? Why do you need to output a formula? Can't you just refer to the column names in the computation?SELECT (Column1 + Column2)/(ColumnA - ColumnB)FROM Table1Tara |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2004-04-13 : 19:57:54
|
quote: Originally posted by tduggan I can't get it to work either. Why can't you compute the value inside the stored proc? Why do you need to output a formula? Can't you just refer to the column names in the computation?SELECT (Column1 + Column2)/(ColumnA - ColumnB)FROM Table1Tara
This is a generic procedure among all shapes in the tbShapes table. The shapes are setup in the front end. The user would create a shape, set the formula, and ensure he/she adds attributes with names matching those in the formula. There has to be a way to do it.Mike B |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2004-04-13 : 20:53:39
|
Ok I see the problem here..In your example you use @fResult float instead of @fResult varchar(500)This works....declare @iTemplate int, @fResult varchar(500)SET @iTemplate = 1EXECUTE usp_shapes_GetCrossSection @iTemplate, @fResult OUTPUTSELECT @fResult = 'SELECT ' + @fResultEXEC (@fResult) DavidM"If you are not my family or friend, then I will discriminate against you" |
|
|
Lavos
Posting Yak Master
200 Posts |
Posted - 2004-04-13 : 21:08:54
|
[Previous Post Deleted]Crap, I A) need to read the other replies closer, B) not accidently hit submit reply before I get everything typed in when I'm switching between windows to double check code, and C) post quick cause otherwise someone will snipe me (like what happened with my first post that I deleted.) (and probably D) stop editing posts over and over after posting)Here was my solution, copying the beginning parts from DavidM :)declare @iTemplate int, @fResult varchar(500)SET @iTemplate = 1EXECUTE usp_shapes_GetCrossSection @iTemplate, @fResult OUTPUTDECLARE @stmt nvarchar(4000)DECLARE @param nvarchar(4000)DECLARE @Eval intSET @stmt='SET @StmResult = ' + @fResultSET @Param='@StmResult int out'EXEC sp_executesql @stmt, @Param, @Eval OUTSELECT @Eval ----------------------"O Theos mou! Echo ten labrida en te mou kephale!""Where theres a will, theres a kludge." - Ken Henderson |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2004-04-13 : 21:21:52
|
quote: Originally posted by byrmol Ok I see the problem here..In your example you use @fResult float instead of @fResult varchar(500)This works....declare @iTemplate int, @fResult varchar(500)SET @iTemplate = 1EXECUTE usp_shapes_GetCrossSection @iTemplate, @fResult OUTPUTSELECT @fResult = 'SELECT ' + @fResultEXEC (@fResult) DavidM"If you are not my family or friend, then I will discriminate against you"
Awsome, pure genius, I didn't even think of dynamic sql. Thank you very much.This, however, will return the result as a result set, is there a way to return this as an output parameter?CREATE PROCEDURE@iTemplate int,@fResult float OUTPUT,ASdeclare @cResult varchar(500)EXECUTE usp_shapes_GetCrossSection @iTemplate, @cResult OUTPUTSELECT @fResult = 'SELECT @fResult = ' + @cResultEXEC (@fResult)GO Of course, the @fResult in red above causes the error:Must declare the variable '@fResult'.Any thoughts on this?Mike B |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2004-04-13 : 21:26:43
|
Mike,Lavos has gone the last mile for you... Use his code!!! sp_executesql is designed for this as it can handle OUTPUT parameters....DavidM"If you are not my family or friend, then I will discriminate against you" |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2004-04-13 : 21:33:29
|
quote: Originally posted by Lavos [Previous Post Deleted]Crap, I A) need to read the other replies closer, B) not accidently hit submit reply before I get everything typed in when I'm switching between windows to double check code, and C) post quick cause otherwise someone will snipe me (like what happened with my first post that I deleted.) (and probably D) stop editing posts over and over after posting)Here was my solution, copying the beginning parts from DavidM :)----------------------"O Theos mou! Echo ten labrida en te mou kephale!""Where theres a will, theres a kludge." - Ken Henderson
Perfection, simply put, thank you very much for your help!Mike B |
|
|
|
|
|
|
|