| Author |
Topic |
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-17 : 11:52:01
|
What does this error mean?--Msg 2786, Level 16, State 1, Procedure sp_GetMovieInfo, Line 61--The data type of substitution parameter 1 does not match the expected type of the format specification. create proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear = nullif exists (select * from Movies where Title like '%@Title%') begin select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock from Movies, Genre, Rating where Title like '%@Title%' and Movies.GenreID = Genre.GenreID and Movies.RatingID = Rating.RatingID return 0 end |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 12:22:18
|
| not sure about error message. but your posted sp has couple of errors. it should becreate proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear int= nullif exists (select * from Movies where Title like '%'+@Title+ '%')beginselect Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStockfrom Moviesinner join Genreon Movies.GenreID = Genre.GenreIDinner join Ratingon Movies.RatingID = Rating.RatingIDwhere Title like '%'+@Title+ '%'return 0endAlso learn to use ANSI joins as other syntax wont be supported in future versions of SQL Server |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-17 : 12:28:03
|
quote: Originally posted by visakh16 not sure about error message. but your posted sp has couple of errors. it should becreate proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear int= nullif exists (select * from Movies where Title like '%'+@Title+ '%')beginselect Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStockfrom Moviesinner join Genreon Movies.GenreID = Genre.GenreIDinner join Ratingon Movies.RatingID = Rating.RatingIDwhere Title like '%'+@Title+ '%'return 0endAlso learn to use ANSI joins as other syntax wont be supported in future versions of SQL Server
Thanks! The error probably stemmed from what you corrected, '%'+@Title+ '%'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 12:33:39
|
| ok. between whats the purpose of @ReleaseYear & @GenreID? i cant see where they are used. If not needed, remove them. |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-17 : 12:36:12
|
quote: Originally posted by visakh16 ok. between whats the purpose of @ReleaseYear & @GenreID? i cant see where they are used. If not needed, remove them.
I need them too because the SP will run a select depending upon which input parameter is given.Here's my other 2 pieces of code:if exists (select * from Movies where GenreID = @GenreID) begin select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock from Movies, Genre, Rating where Movies.GenreID = @GenreID and Movies.GenreID = Genre.GenreID and Movies.RatingID = Rating.RatingID return 0 endif exists (select * from Movies where ReleaseYear = @ReleaseYear) begin select Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStock from Movies, Genre, Rating where Movies.ReleaseYear = @ReleaseYear and Movies.GenreID = Genre.GenreID and Movies.RatingID = Rating.RatingID return 0 end |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 12:39:31
|
there's no need of this if statements you need only the belowcreate proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear int= nullif exists (select * from Movies where Title like '%'+@Title+ '%')beginselect Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStockfrom Moviesinner join Genreon Movies.GenreID = Genre.GenreIDinner join Ratingon Movies.RatingID = Rating.RatingIDwhere (Title like '%'+@Title+ '%' or @Title is null)and (Movies.GenreID = @GenreID or @GenreID is null)and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)return 0end |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-17 : 13:02:21
|
quote: Originally posted by visakh16 there's no need of this if statements you need only the belowcreate proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear int= nullif exists (select * from Movies where Title like '%'+@Title+ '%')beginselect Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStockfrom Moviesinner join Genreon Movies.GenreID = Genre.GenreIDinner join Ratingon Movies.RatingID = Rating.RatingIDwhere (Title like '%'+@Title+ '%' or @Title is null)and (Movies.GenreID = @GenreID or @GenreID is null)and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)return 0end
Sorry, I should have been more specific. Like I want the SELECT to run based on any 1 input parameter.I tried what you provided and if I enter a @Title, it works. But if I enter a @GenreID, it doesn't return any results except return value 0. Same with @ReleaseYear. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-19 : 12:12:37
|
ah...i forgot to remove a bit. try this now and seecreate proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear int= nullbeginselect Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStockfrom Moviesinner join Genreon Movies.GenreID = Genre.GenreIDinner join Ratingon Movies.RatingID = Rating.RatingIDwhere (Title like '%'+@Title+ '%' or @Title is null)and (Movies.GenreID = @GenreID or @GenreID is null)and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)return 0end |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-19 : 20:52:52
|
quote: Originally posted by visakh16 ah...i forgot to remove a bit. try this now and seecreate proc sp_GetMovieInfo@Title varchar(100) = null,@GenreID int = null,@ReleaseYear int= nullbeginselect Title,Genre.Genre,Rating.Rating,ReleaseYear,TotalInStockfrom Moviesinner join Genreon Movies.GenreID = Genre.GenreIDinner join Ratingon Movies.RatingID = Rating.RatingIDwhere (Title like '%'+@Title+ '%' or @Title is null)and (Movies.GenreID = @GenreID or @GenreID is null)and (Movies.ReleaseYear = @ReleaseYear or @ReleaseYear is null)return 0end
Yes this works the way I wanted it, thanks for your help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-20 : 00:26:32
|
Cheers |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-20 : 12:01:38
|
I have another question. I have the following SP. I only want the message 'Video is overdue' to print if the DateDue < GETDATE(). I wasn't sure if I should add another IF statement or if there's an easier way. Thanks,
create proc [dbo].[sp_UpdateRentalRecord]/* input parameters */@MemberID int,@VideoID intasbegin-- validate video idif exists (select 1 from Videos where VideoID = @VideoID) begin --impend a late fee charge if video is returned after due date --the late fee is $1.99 * the number of days after the due date update Customers set Balance = Balance + (1.99*DATEDIFF(dd,rr.DateDue,GETDATE())) FROM Customers c INNER JOIN RentalRecord rr ON rr.MemberID = c.MemberID and rr.DateReturned is null where c.MemberID = @MemberID AND DateDue < GETDATE() print 'Video is overdue' --add the return date of the video update RentalRecord set DateReturned = GETDATE() where VideoID = @VideoID update Movies set TotalInStock = TotalInStock + 1 from Movies, RentalRecord, Videos, Customers where RentalRecord.VideoID = @VideoID and RentalRecord.MemberID = @MemberID and RentalRecord.VideoID = Videos.VideoID and Videos.MovieID = Movies.MovieID update Videos set Videos.VideoStatusID = 1 from Videos, RentalRecord, Movies, Customers where RentalRecord.VideoID = @VideoID and RentalRecord.MemberID = @MemberID and RentalRecord.VideoID = Videos.VideoID and Videos.MovieID = Movies.MovieID endelse begin select 'No video id exists' endend |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-20 : 12:05:05
|
| yup you need another if as datedue is from another table |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-20 : 12:11:08
|
quote: Originally posted by visakh16 yup you need another if as datedue is from another table
ok thanks |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-20 : 14:28:36
|
Another one...thanks in advance!I want to validate for duplicate VideoID insertion.Like when I run sp_AddRentalRecord, I want it to check to see if that VideoID is rented already; if so, then print message and quit.But in my SP, it's printing the message and also running the rest of the SP (which I don't want). I have a IF statement in there, but it doesn't seem to work.RentalRecordRentalIDMemberIDVideoIDDateDueVideosVideoIDMovieIDVideoStatusIDMoviesMovieID
create proc [dbo].[sp_AddRentalRecord]--input parameters (@MemberID int,@VideoID int) asbeginif exists (select 1 from RentalRecord where VideoID = @VideoID and DateDue is null) begin select 'Video id is already rented' return endbegin transaction declare @Total money set @Total = (select Price*1.05 from Videos where VideoID = @VideoID) insert into RentalRecord values (@MemberID,@VideoID,ROUND(@Total,2),GETDATE(), DATEADD(dd,5,GETDATE()),null) -- transaction error handling; if error number is not zero then there's a failure, rollback transaction, and print message if @@error <> 0 begin rollback transaction select 'Insert Failed' return end else begin select 'Rental Record Added Successfully' end commit transactionend |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-21 : 01:59:47
|
put it in elsecreate proc [dbo].[sp_AddRentalRecord]--input parameters (@MemberID int,@VideoID int) asbeginif exists (select 1 from RentalRecord where VideoID = @VideoID and DateDue is null) begin select 'Video id is already rented' return endelsebeginbegin transaction declare @Total money set @Total = (select Price*1.05 from Videos where VideoID = @VideoID) insert into RentalRecord values (@MemberID,@VideoID,ROUND(@Total,2),GETDATE(), DATEADD(dd,5,GETDATE()),null) -- transaction error handling; if error number is not zero then there's a failure, rollback transaction, and print message if @@error <> 0 begin rollback transaction select 'Insert Failed' return end else begin select 'Rental Record Added Successfully' end commit transactionendend |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-21 : 10:13:21
|
quote: Originally posted by visakh16 put it in elsecreate proc [dbo].[sp_AddRentalRecord]--input parameters (@MemberID int,@VideoID int) asbeginif exists (select 1 from RentalRecord where VideoID = @VideoID and DateDue is null) begin select 'Video id is already rented' return endelsebeginbegin transaction declare @Total money set @Total = (select Price*1.05 from Videos where VideoID = @VideoID) insert into RentalRecord values (@MemberID,@VideoID,ROUND(@Total,2),GETDATE(), DATEADD(dd,5,GETDATE()),null) -- transaction error handling; if error number is not zero then there's a failure, rollback transaction, and print message if @@error <> 0 begin rollback transaction select 'Insert Failed' return end else begin select 'Rental Record Added Successfully' end commit transactionendend
Oh that was easy enough. Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-21 : 10:16:23
|
Cheers |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-22 : 13:14:38
|
it should be like belowdont use variables to store values from inserted. inserted may not always contain a single row.create trigger trg_i_customeron Customersfor insertasbeginif exists(select 1 from inserted i inner join Customers c on c.Phone = i.Phone) begin if @@TRANCOUNT > 0 rollback transaction Raiseerror 'Customer with phone number already exists',10,1 endend |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-22 : 13:22:45
|
quote: Originally posted by visakh16 it should be like belowdont use variables to store values from inserted. inserted may not always contain a single row.create trigger trg_i_customeron Customersfor insertasbeginif exists(select 1 from inserted i inner join Customers c on c.Phone = i.Phone) begin if @@TRANCOUNT > 0 rollback transaction Raiseerror 'Customer with phone number already exists',10,1 endend
Thank you!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-22 : 13:23:27
|
Welcome as always |
 |
|
|
sas0riza
Yak Posting Veteran
56 Posts |
Posted - 2008-11-22 : 13:24:32
|
quote: Originally posted by visakh16 it should be like belowdont use variables to store values from inserted. inserted may not always contain a single row.create trigger trg_i_customeron Customersfor insertasbeginif exists(select 1 from inserted i inner join Customers c on c.Phone = i.Phone) begin if @@TRANCOUNT > 0 rollback transaction Raiseerror 'Customer with phone number already exists',10,1 endend
What's the difference between @@error and @@trancount? Thanks |
 |
|
|
Next Page
|