SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On



stored procedure executed successfully but no data in the table Expand / Collapse
Author
Message
Posted Wednesday, December 10, 2008 2:24 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, May 20, 2009 4:53 PM
Points: 134, Visits: 52
Hi all,

I have a simple stored procedure as follow. After I create the procedure. It is successful.
then I execute
Exec pAddNewThemeScheme 4, 'Scheme3', 0, 'Scheme3'
it tells me : Command(s) completed successfully.
but when I check the data in ThemeColor. There is no data over there at all.

I am so confused. It tells me it is successful!

but I can manually insert data with statement:
Insert ThemeColor values(4, 'Scheme3', 0, 'Scheme3')

If I don't have permission to exec stored procedure I created myself. How come it tells me it is successful? what is the possible problem?

Thank you so much!!!

Betty


Create proc [dbo].[pAddNewThemeSCheme] (@themeID int, @label varchar(50), @isActive bit, @folderName varchar(50))
As
--themeID needs to be existant in the table Theme
Declare @TID int
Select @TID=themeID from dbo.Theme where themeID=@themeID
If @TID is NULL
print 'You need to create a theme before your creating theme scheme'
Else
If len(@label)<>0
if len(@label)>50
print 'lable name is too long'
Return
If len(@folderName)<>0
if len(@label) >50
print 'folder name is loo long'
Return;
--if all checks are OK
BEGIN
set @folderName=@folderName+'/'
Insert ThemeColor values(@themeID, @label, @isActive, @folderName)
COMMIT TRANSACTION
END
GO
Post #617403
Posted Wednesday, December 10, 2008 2:40 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:46 AM
Points: 2,148, Visits: 2,511
PRINT statements does't trigger an error. They are just informational messages.

CREATE PROCEDURE dbo.pAddNewThemeSCheme
(
@themeID int,
@label varchar(50),
@isActive bit,
@folderName varchar(50)
)
AS

SET NOCOUNT ON

IF NOT EXISTS (SELECT * FROM dbo.Theme WHERE themeID = @themeID)
BEGIN
RAISERROR('You need to create a theme before your creating theme scheme.', 16, 1)
RETURN -100
END

IF @Label IS NULL OR @Label = ''
BEGIN
RAISERROR('Lable name is missing.', 16, 1)
RETURN
END

IF @folderName IS NULL OR @folderName = ''
BEGIN
RAISERROR('Folder name is missing.', 16, 1)
RETURN
END

IF RIGHT(@folderName, 1) <> '/'
SET @folderName=@folderName + '/'

INSERT ThemeColor
(
themeID,
label,
isActive,
folderName
)
VALUES (
@themeID,
@label,
@isActive,
@folderName
)




N 56°04'39.16"
E 12°55'05.25"
Post #617415
Posted Wednesday, December 10, 2008 5:23 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, May 20, 2009 4:53 PM
Points: 134, Visits: 52
Peso,

thank you so much. You taught me something. That works. The interesting part is my insert statement works in studio directly but not in the stored procedure. Don't know why.

I am wondering if I can put this stored procedure along the folowing statement

Exec pAddNewThemeScheme 4, 'Scheme2', 1, 'Scheme2'

in somesqlscript.sql. So another production database administrator can exec this somesqlscript.sql. It will accomplish creating the stored procedure and insert one record to the database without opening SQL server management studio.

do you have a better way? I don't know much about that.

Betty
Post #617506
Posted Wednesday, December 10, 2008 5:38 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, May 20, 2009 4:53 PM
Points: 134, Visits: 52
Please check with if the following will work.

assume that myScript.sql contains the stored procedure and
Exec pAddNewThemeScheme 4, 'Scheme2', 1, 'Scheme2' statement.


The an adminsitrator can run sqlcmd -S sqlservername\instanceName -i
myScript.sql

or using sql authentication:
sqlcmd -U login_id -P password -S sqlservername\instanceName -i
c:\myScript.sql

Then both stored procedure and one record is created in the database.

Thank you.
Betty
Post #617510
Posted Thursday, December 11, 2008 2:29 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 23, 2009 9:46 PM
Points: 19, Visits: 130
Create proc [dbo].[pAddNewThemeSCheme] (@themeID int, @label varchar(50), @isActive bit, @folderName varchar(50))
As
--themeID needs to be existant in the table Theme
Declare @TID int
Select @TID=themeID from dbo.Theme where themeID=@themeID
If @TID is NULL
print 'You need to create a theme before your creating theme scheme'
Else
If len(@label)<>0
if len(@label)>50
print 'lable name is too long'
Return
If len(@folderName)<>0
if len(@label) >50
print 'folder name is loo long'
Return;

--if all checks are OK


In the above code without using begin - end you are giving multiple statements. So, after checking with condition it is executing Return and coming out successfully.
Post #617678
Posted Friday, December 12, 2008 7:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 08, 2010 10:19 AM
Points: 36, Visits: 78
This part of your code needs to be changed to something like the following, depending on what you are trying to accomplish:

Else
If len(@label)<>0 BEGIN
if len(@label)>50
print 'lable name is too long'
Return
END
If len(@folderName)<>0 BEGIN
if len(@label) >50
print 'folder name is loo long'
Return;
END
--if all checks are OK


If the code to execute after an IF statement is more than one statement it must be surrounded by BEGIN & END. Otherwise, it will a) test for the IF condition, b) if it's true, the first sql statement after the IF will be executed, c) whether it's true or not, then the second statement after the IF is always executed.

So in your code, it tests to see if label is > 50. Then if it is, it prints your statement. Then, whether it is or not it hits the "Return" command and executes it. So it always ends the stored procedure right after that test and it never gets past that.

Hope this helps.

- Cindy
Post #618590
« Prev Topic | Next Topic »


Permissions Expand / Collapse