Showing posts with label straight. Show all posts
Showing posts with label straight. Show all posts

Thursday, March 22, 2012

Computed Column - Error Validating Formula

I want to create a computed column with this formula:

ISNULL(NULLIF (tot_mnc, 0) / NULLIF (repl_value, 0), 0)

It works in a straight select query, but when I put it in the formula of the table design window, I get an error "Error validating the formula for column 'test_fci'"

I don't know if it's relevant but repl_value is itself a computed column with the formula:

(repl_value_e_g + repl_value_aux)

Is it possible to use the system functions in a computed column? If not, how would I pass those values into a udf and use it for the formula?

Thanks.

You are correct: you are not allowed to create a computed column that is based on a different computed column. You can use the built-in functions, so just create your new computed column based on the calculations from the base columns.

If you were to run an ALTER statement to create your computed column based on a computed column you would potentially get an error message like:

Msg 1759, Level 16, State 0, Line xxxx

Computed column 'ccccc' in table 'tttttt' is not allowed to be used in another computed-column definition.

Kent

|||

That was it, thank you. The table designer gui hid the details of the error message and I didn't know which of several possiblities were causing the error.

Thanks again.

Wednesday, March 7, 2012

Complex query, whats the best way?

I have a query that must return information for an invoice. It is not straight forward though. Some things need to be displayed from the if something else is happening...

For example...

declare @.idintdeclare @.chargeidintset @.id = 1set @.chargeid = 9declare @.ppbit, @.pagesint, @.pagefeemoney, @.pagechargemoneyset @.pp = (select perpagefrom tblmainfeeswhere mainfeesid = @.chargeid)if (@.pp = 1)beginset @.pages = (select pagesfrom tblchargesinnerjoin tblrequeston requestid = fkrequestidwhere requestid = @.id)if (@.pages > 0)beginset @.pagefee = (select perpagefeefrom tblmainfeeswhere mainfeesid = @.chargeid)set @.pagecharge = (@.pages * @.pagefee)endend...select @.pagechargeas PerPageCharge

Or should I do some complex query with only one select?

Its hard for anyone to understand if your question is like this: "Some things need to be displayed from the if something else is happening..."

Give us some sample data and your query and what you are expecting to see out of it.

|||

Your right, sorry about that. I slid that one in quick before a meeting...

I will try to explain better.

I have stored procedure that returns data for an invoice, and because Ihate Crystal Reports, I do the most work I can in the stored procedure (formating the text, etc...).

The current query I have involves a 2 complex queries with tons of nested case statements. It is hard to read and does not flow nicely at all, but it works.

I just made enough changes to the database that the stored procedure needs to be rewritten.

I took this opportunity to try and clean up what I wrote prior. I posted a small expample above.

To me, I made it almost "VB" like. Instead of complex select statements with nested logic, I made a variable for each item I needed, and some extra variables for logic. I set each of these variables with it's own select statement, if they passed the logic i had layed out (most of them were initialized with a value). Again, to me, I envisioned each variable being set like a property or a function in VB. I ended up having over 15 simple selects to set variables, and one select at the end to return (get) all of the variables.

The code ended up being much easier to read, and I cut the original code in half, but I greatly increased the amount of select statements. I did some time statistics to see which was faster, and the new query ended up being twice as fast, doing the same work.

My question: Is the way I described and used an accepted way practice? Did I make it some what clearer of what I did or am trying to do?

|||

I would post some data and examples, but I don't think it's relevant. I am not looking for how to do something, but the best way to do something.

For example: "Are you stupid? Don't do it like that.No" or "Yeah, that's fine.Yes"

|||How did you measure the statistics? Just by looking at the time in the query analyzer window? Check out some of the SET STATISTICS IO ON and SET STATISTICS TIME ON statements in books on line. then do the compare for before and after. If you are more comfortable with Profiler, get the aggregate reads on each of the tables. See which method requires less reads. From what you described it looks like you are reading the same tables more than once for each of those variables. I would get all the values you need into variables in as many few SELECTs as possible. Then work on the variables with all the formatting. Again, formatting is better done at the reporting layer. I think you use VB code in crystal reports right? So if you are more comfortable in VB you might want to move the formatting to the CR layer.