Showing posts with label types. Show all posts
Showing posts with label types. Show all posts

Friday, March 30, 2012

how to control data types

i have sp that insert fileName and fileType

how can i forbidden in the sp other types then word doc and images?

Code Snippet

if not exists(select Number

from dbo.Freezrelease

where FileName1 like @.FileName1 and

FileType like @.FileType and

FileSize = @.FileSize)

/*{*/ begin

if @.FileSize<500

/*{*/ begin

insert into dbo.Freezrelease(FileName1,FileType,FileSize)

values(@.FileName1,@.FileType,@.FileSize)

select @.@.identity

/*}*/ end

else

select -1

/*}*/end

else

select 0

So you are just inserting the file names, not the bits? If so, just add a line:

if @.fileType not in ('doc','jpg','...etc')

begin

raiserror ('Filetype must be doc, jpg, or etc')

return -100

end

And the procedure will stop there after raising an error

|||

You could add a CHECK constraint to the table that limits the values in the FileName field to only values ending with '.doc', '.docx'*, or '.jpg'. (And of course, any other image file extensions required...)

Using this method, it would not be necessary to 'control' every stored procedure or application query. Programmers and users will not be able to thwart your determination that a 'proper' FileName be supplied. The CHECK constraint would 'police' the inserts/updates for you.

* .docx for Word 2007

Code Snippet


CREATE TABLE #MyTable
( RowID int IDENTITY,
FileName varchar(50)
CHECK ( FileName LIKE '%.doc%' OR FileName LIKE '%.jpg' )
)
GO


-- These succeed
INSERT INTO #MyTable VALUES ( 'MyFile.doc' )
INSERT INTO #MyTable VALUES ( 'MyPic.jpg' )
INSERT INTO #MyTable VALUES ( 'MyFile.docx' )


-- This fails
INSERT INTO #MyTable VALUES ( 'Myfile.txt' )


-- This fails
INSERT INTO #MyTable VALUES ( 'Myfile' )


SELECT *
FROM #MyTable

RowID FileName
-- --
1 MyFile.doc
2 MyPic.jpg
3 MyFile.docx


DROP TABLE #MyTable

|||

What Arnie says is true too, though I would prefer having a fileType column to hold the extension also, for all of the basic normalization reasons:

1. You can add information about types of files easily and join to it without the substring

2. Listing files by type will not require a substring

3. Searching for files by type can be indexed

Really it is all about the need to avoid dealing with parts of a column value.

Monday, March 19, 2012

How to configure Smo.Scripter to get desired sprocs

Hi, I've developed a sproc generator using SMO types and all works fine except when I want to generate sprocs prefaced by a drop statement. To do this I create an instance of the Scripter object and set it's options.ScriptDrops property to true ( instance.options.ScriptDrops = true).

When options.ScriptDrops = true the output omits the actual stored procedure from the scriopt. When I comment out scriptDrops = true it works correctly. How do i get it working correctly WITH drop statements?

Example:

private void CreateScripter() {
if( this._SprocSscripter == null ) {
this._SprocSscripter = new Scripter();
this._SprocSscripter.Server = this._tables.Parent.Parent;
this._SprocSscripter.Options.IncludeHeaders = true;
this._SprocSscripter.Options.IncludeIfNotExists = true;
this._SprocSscripter.Options.DdlBodyOnly = false;
this._SprocSscripter.Options.DdlHeaderOnly = false;
this._SprocSscripter.Options.ExtendedProperties = true;
this._SprocSscripter.Options.ScriptDrops = true;
}
}

OUTPUT:

/****** Object: StoredProcedure [dbo].[usp_selectCategories] Script Date: 01/18/2007 16:39:29 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_selectCategories]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_selectCategories]

OUPUT when i comment out last line ( this._SprocSscripter.Options.ScriptDrops = true;)

/****** Object: StoredProcedure [dbo].[usp_selectCategories] Script Date: 01/18/2007 16:37:04 ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_selectCategories]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @.statement = N'CREATE PROCEDURE [dbo].[usp_selectCategories]
@.CategoryID [int]
AS
SELECT [CategoryID],
[CategoryName],
[Description],
[Picture]
FROM [dbo].[Categories]
WHERE [CategoryID] = @.CategoryID
'
END

If I Comment out all lines in the CreateScripter method exect the 1st, 2nd and last ( this._SprocSscripter.Options.ScriptDrops = true;)
The output looks like this:

DROP PROCEDURE [dbo].[usp_selectCategories]

That's it-- no sproc body.. what gives?

Bill Graziano did a great job of documenting how to do this at his SQLTeam.com web site - here's the link to the article:

http://www.sqlteam.com/item.asp?ItemID=23185

|||Excellent. Thank you. Too bad it's a bug, but learning that it is has restored some of my sanity :-)|||

|||

How to configure Smo.Scripter to get desired sprocs

Hi, I've developed a sproc generator using SMO types and all works fine except when I want to generate sprocs prefaced by a drop statement. To do this I create an instance of the Scripter object and set it's options.ScriptDrops property to true ( instance.options.ScriptDrops = true).

When options.ScriptDrops = true the output omits the actual stored procedure from the scriopt. When I comment out scriptDrops = true it works correctly. How do i get it working correctly WITH drop statements?

Example:

private void CreateScripter() {
if( this._SprocSscripter == null ) {
this._SprocSscripter = new Scripter();
this._SprocSscripter.Server = this._tables.Parent.Parent;
this._SprocSscripter.Options.IncludeHeaders = true;
this._SprocSscripter.Options.IncludeIfNotExists = true;
this._SprocSscripter.Options.DdlBodyOnly = false;
this._SprocSscripter.Options.DdlHeaderOnly = false;
this._SprocSscripter.Options.ExtendedProperties = true;
this._SprocSscripter.Options.ScriptDrops = true;
}
}

OUTPUT:

/****** Object: StoredProcedure [dbo].[usp_selectCategories] Script Date: 01/18/2007 16:39:29 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_selectCategories]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_selectCategories]

OUPUT when i comment out last line ( this._SprocSscripter.Options.ScriptDrops = true;)

/****** Object: StoredProcedure [dbo].[usp_selectCategories] Script Date: 01/18/2007 16:37:04 ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_selectCategories]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @.statement = N'CREATE PROCEDURE [dbo].[usp_selectCategories]
@.CategoryID [int]
AS
SELECT [CategoryID],
[CategoryName],
[Description],
[Picture]
FROM [dbo].[Categories]
WHERE [CategoryID] = @.CategoryID
'
END

If I Comment out all lines in the CreateScripter method exect the 1st, 2nd and last ( this._SprocSscripter.Options.ScriptDrops = true;)
The output looks like this:

DROP PROCEDURE [dbo].[usp_selectCategories]

That's it-- no sproc body.. what gives?

Bill Graziano did a great job of documenting how to do this at his SQLTeam.com web site - here's the link to the article:

http://www.sqlteam.com/item.asp?ItemID=23185

|||Excellent. Thank you. Too bad it's a bug, but learning that it is has restored some of my sanity :-)