Showing posts with label inside. Show all posts
Showing posts with label inside. Show all posts

Wednesday, March 7, 2012

Enclosing a table create inside BEGIN END

The following SQL (after the "The SQL" marker) works fine. But I want to enclose it in a check to make sure that a previous script hasn't already created this object and its CRUD.

If I enclose it in a block like this...

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ZIPCODE]') AND type in (N'U'))
BEGIN

blah blah blah

END

It won't parse. It seems to me like this should be a no brainer, but it is hurting my brain.

The SQL...

CREATE TABLE [dbo].[ZIPCODE](
[Zip] [nvarchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Latitude] [nvarchar](12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Longitude] [nvarchar](12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[City] [nvarchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[State] [nvarchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[County] [nvarchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Zip_class] [nvarchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_ZIPCODE] PRIMARY KEY CLUSTERED
(
[Zip] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

/* Zip Code CRUD */
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetZipLocationsWithinBounds]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetZipLocationsWithinBounds]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetZipLocationsByCityState]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetZipLocationsByCityState]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetZipLocation]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetZipLocation]
GO
CREATE PROCEDURE [dbo].[GetZipLocationsWithinBounds]
@.TopLine float,
@.LeftLine float,
@.Bottomline float,
@.RightLine float
AS
SELECT * FROM ZIPCODE
WHERE
LATITUDE >= @.Bottomline AND
LATITUDE <= @.TopLine AND
LONGITUDE >= @.LeftLine AND
LONGITUDE <= @.RightLine
ORDER BY State, City ASC
GO

CREATE PROCEDURE [dbo].[GetZipLocationsByCityState]
@.City varchar(25),
@.State varchar(2)
AS
SELECT * FROM ZIPCODE
WHERE
City = @.City AND
State = @.State
ORDER BY State, City ASC
GO

CREATE PROCEDURE [dbo].[GetZipLocation]
@.ZipCode varchar(10)
AS
SELECT * FROM ZIPCODE
WHERE
Zip = @.ZipCode
GO

Hi there,

This error is caused by a GO keyword. The GO command force the SQL Interpreter to execute the previous part of your code block! so if the Interpreter try to execute your first create command your IF block make this error.

If your statements doesn't have any dependency to each other your simply solve this problem by removing the GO commands; But if there is some dependency you must break them in an independent part.

Babak Izadi
LotraSoft Ltd.

|||

You should not use GO inside your T-SQL Batch statement.

Go is not a T-SQL Statement. SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to SQL Server.

Remove all the GO statements & put it in your BEGIN .. END .. It will work ..

Friday, February 24, 2012

Enable tcp/ip protocol via registry

Well, I need to enable the tcp/ip protocol via registry,
so I added the following value inside the registry
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsof t SQL
Server\GESTP\MSSQLServer\SuperSocketNetLib\Tcp]
"TcpHideFlag"=dword:00000000
"TcpDynamicPorts"="0"
"TcpPort"="1433"
But if I call the svrnetcn tool tcp/ip isn't listed as enable protocol.
Why?
Andrea Moro
I forgot to say ... I want to enable the tcp/ip protcol
for a named instance.
I suspect that sql tool do more thing that alter the
registry keys I specified.
Andrea
|||hi Andrea,
"Andrea Moro" <moroandreaet@.ettiscali.it> ha scritto nel messaggio
news:%23dg$jOX2EHA.2180@.TK2MSFTNGP10.phx.gbl
> Well, I need to enable the tcp/ip protocol via registry,
> so I added the following value inside the registry
> [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsof t SQL
> Server\GESTP\MSSQLServer\SuperSocketNetLib\Tcp]
> "TcpHideFlag"=dword:00000000
> "TcpDynamicPorts"="0"
> "TcpPort"="1433"
> But if I call the svrnetcn tool tcp/ip isn't listed as enable
> protocol.
> Why?
you have to check
HKLM\SOFTWARE\Microsoft\Microsoft SQL
Server\InstanceName\MSSQLServer\SuperSocketNetLib
\ProtocolList
key, that must include the "tcp" value
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply