| Author |
Topic |
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-29 : 16:21:56
|
| My task is to write a stored procedure for the followingI have 2 tables name Sales.Currency and Sales.Currency Rate'Sales.Currency has t2 colums name of currency and the currency codeSales.CurrencyRate has colums name Currency Code, From Currency Code,To Currency Code, Average Rate, Currency exchange Rate.all the values in From CurrencyCode Column is U.S.dollarFollwing input parameters , NAme of the currency,Amount to be converted, Name of the currency in which it should be converted, Currency exchange rate dateOutput parameterConverted amountFollowing is my codeUse AdventureWorksGoAlter Procedure Currency @nameofcurr nvarchar(50),@amountgiven decimal(38,0),@convertcurrency nvarchar(50),@Date datetime,@convertedamt decimal(38,0) outputAsDeclare @a nvarchar(10)Declare @b nvarchar(10)Declare @c decimal (38,0)Select @a= SC.CurrencyCode From Sales.Currency as SCWhere @nameofcurr = SC.[Name] Select @b = Sc.CurrencyCode From Sales.Currency as SCWhere @convertcurrency = SC.[Name]Select @Date = SCR.CurrencyRateDate,@c = SCR.AverageRate From Sales.CurrencyRate as SCR Inner JoinSales.Currency as SC On SC.Currencycode = SCR. ToCurrencyCodeWhere @b = SCR.ToCurrencyCode Set @convertedamt = @amountgiven * @cDECLARE @convertedamt decimal (38,0)Execute Currency USDollar, $1,CanadianDollar,6 July 2001 12 a.m.,@convertedamt outputPrint @convertedamtThe error i get is Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'July'Can you let me know where i am going wrong |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-10-29 : 16:31:24
|
| Umm. Wow.Your execute statement isn't written in a way that can be executed, what are you intending the "Execute CUrrency USDollar, $1, CanadianDollar,6 July 2001 12 a.m" to do? |
 |
|
|
tm
Posting Yak Master
160 Posts |
Posted - 2007-10-29 : 16:43:54
|
| You should refer to Books On Line or research into executing stored procedures.Basic error with Execute .. Use single quotes with strings and dates. |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-29 : 17:31:58
|
| I changed my execution statementDECLARE @convertedamt decimal (38,0)Execute Currency 'USDollar', 2,'CanadianDollar', '2001.02.07 12AM',@convertedamt outputPrint @convertedamtTHe query gets executed but does not show any value |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 03:00:55
|
Maybe it doesn't find any rows?Put some debug code in:Select @a= SC.CurrencyCodeFrom Sales.Currency as SCWhere @nameofcurr = SC.[Name]SELECT '@a Query', [@nameofcurr] = @nameofcurr, [@a] = @a, [Rowcount] = @@ROWCOUNT and so on so you can see what is going on. Once you've debugged it you can remove, or comment-out, the debug statementsKristen |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 14:17:48
|
| Thank you for guidance but my code is stillnot executingThe problem is that @c does not pick up the value from the tableI tried debug the code and know that it is not picking up the value but not able to ascertain whyPlease can you let me know where i am wrong in my codeThe code is as followsUse AdventureWorksGoAlter Procedure Currency @nameofcurr nvarchar(50),@amountgiven decimal(38,4),@convertcurrency nvarchar(50),@Date datetime,@convertedamt decimal(38,4) outputAsDeclare @a nvarchar(10)Declare @b nvarchar(10)Declare @c decimal (38,4)Select @a= SC.CurrencyCode From Sales.Currency as SCWhere @nameofcurr = SC.[Name]Select @b = SC.CurrencyCode From Sales.Currency as SCWhere @convertcurrency = SC.[Name]Select @c=SCR.EndofDayRate From Sales.CurrencyRate as SCR WhereSCR.CurrencyRateDate = @Date --and--SCR.ToCurrencyCode = @b SELECT '@c Query', [@c] = @c, [@c] = @c, [Rowcount] = @@ROWCOUNTSet @convertedamt = @amountgiven * @cWhen executed it shows that variable c has null value |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 14:22:41
|
| Select @c=SCR.EndofDayRate From Sales.CurrencyRate as SCRWhere SCR.CurrencyRateDate = @DateYou haven't set @Date to a value, so it won't match anything ... |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 14:25:47
|
| @date is an input parameterso when i a m executing the procedure with follwing codeDECLARE @convertedamt decimal (38,4)Execute Currency 'US Dollar', 1,'Yen', '2001.01.07 12:00:00AM',@convertedamt outputPrint @convertedamtIt does not return any value |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 14:34:45
|
"@date is an input parameter"Sorry, I misread your original post What is the output fromSELECT '@c Query', [@c] = @c, [@Date] = @Date, [@b] = @b, [Rowcount] = @@ROWCOUNT(inserted in place of your current debug statement) please?Kristen |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 14:48:14
|
| follwing is the output@c -------- @c Query NULL @Date ---------2001-01-07 00:00:00.000 @b ----------JPY Rowcount------------0(1 row(s) affected) |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-10-30 : 14:52:09
|
| Does the date exist in your CurrencyRate table. If it tries to set the CUrrencyRate based on the EndofDayRate in that table, where the CurrencyRateDate = @Date and the CurrencyRateDate has no match, the value will be null. Either that or the EndofDayRate in the table for that date is null.sorry to jump in here. |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 15:15:15
|
| follwing is the output@c -------- @c Query NULL @Date ---------2001-01-07 00:00:00.000 @b ----------JPY Rowcount------------0(1 row(s) affected) |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 15:17:57
|
| Please do not say sorry. Rather I would like to thank you for helping me It does have that date i will just send the exceprt from the table1 7/1/2001 12:00:00 AM USD ARS 1.0000 1.00022 7/1/2001 12:00:00 AM USD AUD 1.5491 1.55003 7/1/2001 12:00:00 AM USD BRL 1.9379 1.94194 7/1/2001 12:00:00 AM USD CAD 1.4641 1.46835 7/1/2001 12:00:00 AM USD CNY 8.2781 8.27846 7/1/2001 12:00:00 AM USD DEM 1.8967 1.89767 7/1/2001 12:00:00 AM USD EUR 0.9697 0.97038 7/1/2001 12:00:00 AM USD FRF 6.3611 6.36139 7/1/2001 12:00:00 AM USD GBP 0.6183 0.618310 7/1/2001 12:00:00 AM USD JPY 104.9100 104.958011 7/1/2001 12:00:00 AM USD MXN 9.3740 9.384012 7/1/2001 12:00:00 AM USD SAR 3.7507 3.758413 7/1/2001 12:00:00 AM USD VEB 634.5099 634.600014 7/2/2001 12:00:00 AM USD ARS 1.0000 0.999115 7/2/2001 12:00:00 AM USD AUD 1.5559 1.555816 7/2/2001 12:00:00 AM USD BRL 1.9339 1.933017 7/2/2001 12:00:00 AM USD CAD 1.4661 1.463718 7/2/2001 12:00:00 AM USD CNY 8.2781 8.277419 7/2/2001 12:00:00 AM USD DEM 1.8924 1.8922 |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-10-30 : 15:20:10
|
| @C is null. That means your query to populate that variable is not returning anything. Check the source data to make sure either the date actually exists, the end of day currency rate exists, or that your JOIN actually joins the data together properly. |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 15:20:48
|
| First column is Currency Id, CurrencyRateDate,ToCurrencyCode,AverageRate,EndOfDayRate |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-10-30 : 16:20:28
|
| Assuming the SALES.Currency is a table, and SALES.CurrencyRate are separate tables, your output for: @Date---------2001-01-07 00:00:00.000 does not match your intended match of2001-01-07 12:00:00 AMSince they are different, your @c variable will populate with null.I would do : Select @c=SCR.EndofDayRate From Sales.CurrencyRate as SCRWhere Cast(SCR.CurrencyRateDate as smalldatetime) = Cast(@Date as smalldatetime)As predicted, the dates don't match exactly, so the above part of your code as you have will not produce a result for @c |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 16:58:15
|
Probably worth checking if there are any "near" values:Select TOP 100 *From Sales.CurrencyRate as SCRWhere SCR.CurrencyRateDate >= '20010107' AND SCR.CurrencyRateDate < '20010108' If you don't get any rows then there are no entries in your Sales.CurrencyRate table with that date.Are you sure that your [CurrencyRate] table should be OWNED BY [Sales] ??Kristen |
 |
|
|
ganatra.neha
Starting Member
30 Posts |
Posted - 2007-10-30 : 17:38:34
|
| Thank you it worksit was date and time format problem. I did as you guided me.Thank you once again |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 18:14:48
|
If SCR.CurrencyRateDate contains Time, as well as Date, then you can select everything on a given date using:Select @c=SCR.EndofDayRate From Sales.CurrencyRate as SCRWhere SCR.CurrencyRateDate >= DATEADD(Day, DATEDIFF(Day, 0, @Date), 0) AND SCR.CurrencyRateDate < DATEADD(Day, DATEDIFF(Day, 0, @Date)+1, 0) to demonstrate this:SELECT DATEADD(Day, DATEDIFF(Day, 0, GetDate()), 0) AS [Start], DATEADD(Day, DATEDIFF(Day, 0, GetDate())+1, 0) AS [End] Start will be midnight last night, and End will be midnight tonight.Kristen |
 |
|
|
|