Showing posts with label stored. Show all posts
Showing posts with label stored. Show all posts

Thursday, March 29, 2012

Embedded Images Not Displaying properly

Hi,

I have a question which i hope someone can help with. When using an embedded image stored on a server in my reporting services report i can see it when proeviewing the report but when it is deployed it no longer is shown on the report. If i try this with an image stored on my local drive it works fine in both preview and deployed mode.

I am wondering why this could be? My initial thoughts are that the security on the file stored on the server may not allow access when the report is run in a deployed state yet it is accessible to the report developper as they have access to the server directory. Does this sound feasible and if not can someone let me know as to why this may not work.

Many thanks,

Grant

Grant,

if you point to the picture just as an resource you should also publish the picture to the proper RS folder like the report.

Or use a web link which should be a "public" one and has to be accessible by every User using RS.

cheers,
Markus

embedded images in the report

We have company logo stored as an "embedded image" in the rdl file. When the
report is deployed to the production server, the logo is not displayed in
the Report Manager.
What all steps are required to deploy reports with embedded images?
pls. help.
ThanksHi newmem,
I have had the same problem. I have found out that my IE 6.0 causes the
error in my case. After switching the IE options "check for newer version of
stored html pages" from "automatically" to "every visit to the page"
everything works fine...
your way: IE -> Internet Options > Settings
I identified the IE because every output format displays the company logo
correctly (PDF...) and only the default and html format was wrong...
Regards
Wolfgang Himmelsbach
"newmem" wrote:
> We have company logo stored as an "embedded image" in the rdl file. When the
> report is deployed to the production server, the logo is not displayed in
> the Report Manager.
> What all steps are required to deploy reports with embedded images?
> pls. help.
> Thanks
>
>sql

Tuesday, March 27, 2012

Embedded Code Executing SQL

I'm trying to execute a stored procedure in the header section of a report
(so I can't use a dataset in the data tab) using the shared datasource of the
report. Does anyone have a simple example executing SQL from the embedded
code of a report?Bryan,
You can use the objects from the detail section to show in the header
or...try to have a select statement in the code behind function.
if u are okay with the first line, just pop up again, i will check and give
u the code
"Bryan" wrote:
> I'm trying to execute a stored procedure in the header section of a report
> (so I can't use a dataset in the data tab) using the shared datasource of the
> report. Does anyone have a simple example executing SQL from the embedded
> code of a report?

Embarrassingly simple question about using stored proceedures

The stored procedure sp_columns returns several fields but i want just the
COLUMN_NAME
if the stored procedure was a table I would write the query
Select COLUMN_NAME from sp_columns
Whats the simplest way of doing this for a stored proceedure
many thanks
David HEither re-write the procedure or use below technique:
CREATE TABLE #tmp...
INSERT #tmp
EXEC sp_columns
SELECT ... FROM #tmp
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"David Hills" <dhills@.pcfe.ac.uk> wrote in message
news:BEEA10DA-869E-4A9B-B884-56B6070B6725@.microsoft.com...
> The stored procedure sp_columns returns several fields but i want just the
COLUMN_NAME
> if the stored procedure was a table I would write the query
> Select COLUMN_NAME from sp_columns
> Whats the simplest way of doing this for a stored proceedure
> many thanks
> David H
>|||An even simpler approach would be to ditch sp_columns and use the
INFORMATION_SCHEMA views instead. For example, the get the columns of the
Authors table in the pubs database use:
USE pubs
GO
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_CATALOG = 'pubs'
AND TABLE_NAME = 'authors'
or
SELECT COLUMN_NAME
FROM pubs.INFORMATION_SCHEMA.Columns
WHERE TABLE_CATALOG = 'pubs'
AND TABLE_NAME = 'authors'
You can get more information INFORMATION_SCHEMA views in BOL:
http://msdn.microsoft.com/library/d...br />
4pbn.asp
Cheers,
Stefan
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:OmygI4YDEHA.628@.TK2MSFTNGP10.phx.gbl...
> Either re-write the procedure or use below technique:
> CREATE TABLE #tmp...
> INSERT #tmp
> EXEC sp_columns
> SELECT ... FROM #tmp
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
>
> "David Hills" <dhills@.pcfe.ac.uk> wrote in message
> news:BEEA10DA-869E-4A9B-B884-56B6070B6725@.microsoft.com...
the
> COLUMN_NAME
>|||That's a good point, Stefan!
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Stefan Delmarco [MSFT]" <StefanDe@.online.microsoft.com> wrote in messag
e
news:eAuYCTZDEHA.3240@.TK2MSFTNGP10.phx.gbl...
> An even simpler approach would be to ditch sp_columns and use the
> INFORMATION_SCHEMA views instead. For example, the get the columns of the
> Authors table in the pubs database use:
> USE pubs
> GO
> SELECT COLUMN_NAME
> FROM INFORMATION_SCHEMA.Columns
> WHERE TABLE_CATALOG = 'pubs'
> AND TABLE_NAME = 'authors'
> or
> SELECT COLUMN_NAME
> FROM pubs.INFORMATION_SCHEMA.Columns
> WHERE TABLE_CATALOG = 'pubs'
> AND TABLE_NAME = 'authors'
> You can get more information INFORMATION_SCHEMA views in BOL:
>
http://msdn.microsoft.com/library/d..._ia-iz_4pbn.asp[
color=darkred]
> Cheers,
> Stefan
> --
> This posting is provided "AS IS" with no warranties, and confers no[/color]
rights.
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote
in
> message news:OmygI4YDEHA.628@.TK2MSFTNGP10.phx.gbl...
> the
>|||Many thanks, that's just what I needed to know to do this
SELECT TABLE_NAME,COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_CATALOG = 'sipr'
AND COLUMN_NAME LIKE '%rlg%'
TABLE_NAME COLUMN_NAME
--
d21_eed44 rlg_code
D04_RLG rlg_code
D04_RLG rlg_name
D04_RLG rlg_snam
SRS_RLG rlg_code
SRS_RLG rlg_name
SRS_RLG rlg_snam
D04_STU stu_rlgc
D04_CONTCT stu_rlg
D51_STU_CTS stu_rlgc
D51_STU_V26 stu_rlgc
INS_STU stu_rlgc

Thursday, March 22, 2012

email using

hi
can any one guide me, how to send email using stored procedure in sql server
2000 in windows 2003 o/s without using mail client installed on server.
Thanks
KalyanUse xp_smtp_sendmail, all you need is the ability to install an extended
procedure, and an SMTP server you can control.
http://www.aspfaq.com/2403
"Kalyan" <Kalyan@.discussions.microsoft.com> wrote in message
news:6CF632BE-9936-4E86-8477-297EBEFC952A@.microsoft.com...
> hi
> can any one guide me, how to send email using stored procedure in sql
> server
> 2000 in windows 2003 o/s without using mail client installed on server.
> Thanks
> Kalyan
>|||Aaron
My operating system is windows 2003, Is smtp comes with IIS?
Thanks
Kalyan
"Aaron Bertrand [SQL Server MVP]" wrote:

> Use xp_smtp_sendmail, all you need is the ability to install an extended
> procedure, and an SMTP server you can control.
> http://www.aspfaq.com/2403
>
> "Kalyan" <Kalyan@.discussions.microsoft.com> wrote in message
> news:6CF632BE-9936-4E86-8477-297EBEFC952A@.microsoft.com...
>
>|||> My operating system is windows 2003, Is smtp comes with IIS?
IIS has its own SMTP server, so yes, I guess it is compatible. However, you
do not have to have SMTP running on the same server as SQL Server, in fact I
recommend it be separate.

E-Mail User When StoredProc Fails

Hi,

I want to e-mail a user when a Stored Proc fails, what is the best way to do this? I was going to create a DTS package or is this too complicated?

Also, the Stored Proc inserts data from one table to another, I would like to use Transactions so that if this fails it rolls back to where it was, I'm not sure of the best way to go about this. Could anyone possibly point me in the right direction? Here's a copy of some of the stored procedure to give an idea of what I am doing:

-- insert data into proper tables with extract date added
INSERT INTO tbl_Surgery
SELECT
SurgeryKey,
GETDATE(),
ClinicianCode,
StartTime,
SessionGroup,
[Description],
SurgeryName,
Deleted,
PremisesKey,
@.practiceCode --SUBSTRING(SurgeryKey,PATINDEX('%.%',SurgeryKey)+1, 5)
FROM tbl_SurgeryIn

INSERT INTO tbl_SurgerySlot
SELECT
SurgerySlotKey,
GETDATE(),
SurgeryKey,
Length,
Deleted,
StartTime,
RestrictionDays,
Label,
IsRestricted,
@.practiceCode
FROM tbl_SurgerySlotIn

INSERT INTO tbl_Appointment
SELECT
AppointmentKey,
GETDATE(),
SurgerySlotKey,
PatientKey,
Cancelled,
Continuation,
Deleted,
Reason,
DateMade
FROM tbl_AppointmentIn

-- empty input tables
DELETE FROM tbl_SurgeryIn
DELETE FROM tbl_SurgerySlotIn
DELETE FROM tbl_AppointmentIn

Any help would me very much appreciated,

ThanksSomething like this should work:

CREATE PROCEDURE ProcName

AS

BEGIN TRANSACTION transaction_1

DECLARE @.error_handle VARCHAR(255)

-- insert data into proper tables with extract date added
INSERT INTO tbl_Surgery
SELECT
SurgeryKey,
GETDATE(),
ClinicianCode,
StartTime,
SessionGroup,
[Description],
SurgeryName,
Deleted,
PremisesKey,
@.practiceCode --SUBSTRING(SurgeryKey,PATINDEX('%.%',SurgeryKey)+1, 5)
FROM tbl_SurgeryIn

IF @.@.ERROR <> 0
BEGIN
SELECT @.error_handle = 'ProcName::Failure on tbl_Surgery insert.'
GOTO error_handle
END

INSERT INTO tbl_SurgerySlot
SELECT
SurgerySlotKey,
GETDATE(),
SurgeryKey,
Length,
Deleted,
StartTime,
RestrictionDays,
Label,
IsRestricted,
@.practiceCode
FROM tbl_SurgerySlotIn

IF @.@.ERROR <> 0
BEGIN
SELECT @.error_handle = 'ProcName::Failure on tbl_SurgerySlot insert.'
GOTO error_handle
END

INSERT INTO tbl_Appointment
SELECT
AppointmentKey,
GETDATE(),
SurgerySlotKey,
PatientKey,
Cancelled,
Continuation,
Deleted,
Reason,
DateMade
FROM tbl_AppointmentIn

IF @.@.ERROR <> 0
BEGIN
SELECT @.error_handle = 'ProcName::Failure on tbl_Appointment insert.'
GOTO error_handle
END

-- empty input tables
DELETE FROM tbl_SurgeryIn

IF @.@.ERROR <> 0
BEGIN
SELECT @.error_handle = 'ProcName::Failure on tbl_Surgery delete.'
GOTO error_handle
END

DELETE FROM tbl_SurgerySlotIn

IF @.@.ERROR <> 0
BEGIN
SELECT @.error_handle = 'ProcName::Failure on tbl_SurgerySlotIn delete.'
GOTO error_handle
END

DELETE FROM tbl_AppointmentIn

IF @.@.ERROR <> 0
BEGIN
SELECT @.error_handle = 'ProcName::Failure on tbl_AppointmentIn delete.'
GOTO error_handle
END

end_procedure:
COMMIT TRANSACTION transaction_1 --Commits transactions if no errors occurred.
RETURN 0 --Indicates succcess.

error_handle
ROLLBACK TRANSACTION transaction_1
RAISERROR(@.error_handle,16,1)
EXEC xp_sendmail 'user@.mail.com',@.error_handle
RETURN 1

Alternately, if you are running this from a job, you can strip out the xp_sendmail and just have it send email on failure. The RETURN 1 with the RAISERROR will indicate failure.|||I would strongly suggest creating a job to run the stored procedure, and having the job email you on failure. It is easy to do, and relatively foolproof!

-PatP|||Thanks, that's great, I'll give it a go. So the RAISERROR and RETURN 1 are just a way of letting SQL know that the procedure has failed??

The only thing I'm confused/worried about is the best place to put the BEGIN TRANSACTION and the end_procedure code. The stored procedure I've inherited has rather a lot of BEGINS/ENDS so I'm worried about confusing it, here's a copy of the original, any indication you could give me would really help:

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE sproc_48hrAccess_Upload

AS

SET DATEFORMAT dmy

DECLARE @.practiceCode char(5)
DECLARE @.server varchar(255)
DECLARE @.inPath varchar(255)
DECLARE @.archiveBase varchar(255)
DECLARE @.archivePath varchar(255)
DECLARE @.zipPath varchar(255)
DECLARE @.cmdshell varchar(255)
DECLARE @.result int
DECLARE @.date varchar(10)

SET @.server = 'MURDOCH'
SET @.inPath = 'E:\48hrAccess\48hrDataIn\'
SET @.archiveBase = 'E:\48hrAccess\48hrDataArchive\'
SET @.zipPath = 'C:\Progra~1\WinZip\' --C:\Program Files\Winzip\
SET @.date = CONVERT(varchar(2),DATEPART(dd,GETDATE()))+CONVERT (varchar(2),DATEPART(mm,GETDATE()))+CONVERT(char(4 ),DATEPART(yy,GETDATE()))

-- upload for each practice in tbl_Practice
DECLARE allPractices CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR SELECT practiceCode FROM tbl_Practice
OPEN allPractices
FETCH NEXT FROM allPractices INTO @.practiceCode

WHILE @.@.FETCH_STATUS = 0
BEGIN
SET @.archivePath = @.archiveBase+@.practiceCode+'\'
-- copy files into archive folder
SET @.cmdshell = 'MOVE '+@.inPath+'48hr_'+@.practiceCode+'_'+@.date+'.zip '+@.archivePath
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

-- unzip file
SET @.cmdshell = @.zipPath+'WZUNZIP -ybc -o -sPASSWORD '+@.archivePath+'48hr_'+@.practiceCode+'_'+@.date+'.z ip '+@.archivePath
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

-- copy data files into upload tables
SET @.cmdshell = 'ECHO ** BEGIN Upload '+CONVERT(varchar,GETDATE())+' ******************************* >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

SET @.cmdshell = 'ECHO xv_Surgery.dat >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT
SET @.cmdshell = 'bcp GMS_48hrAccess..tbl_SurgeryIn in '+@.archivePath+'xv_Surgery.dat -n -V65 -t"||" -r"|||\n" -S'+@.server+' -T >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

SET @.cmdshell = 'ECHO xv_SurgerySlot.dat >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT
SET @.cmdshell = 'bcp GMS_48hrAccess..tbl_SurgerySlotIn in '+@.archivePath+'xv_SurgerySlot.dat -n -V65 -t"||" -r"|||\n" -S'+@.server+' -T >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

SET @.cmdshell = 'ECHO xv_Appointment.dat >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT
SET @.cmdshell = 'bcp GMS_48hrAccess..tbl_AppointmentIn in '+@.archivePath+'xv_Appointment.dat -n -V65 -t"||" -r"|||\n" -S'+@.server+' -T >> '+@.archivePath+'48hrUpload.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

-- clean up
SET @.cmdshell = 'DEL /F '+@.archivePath+'*.dat '+@.archivePath+'48hrAccess.log'
EXEC master..xp_cmdshell @.cmdshell, NO_OUTPUT

-- update tbl_SurgerySlotDescription (will fire trigger if new labels appear)
INSERT INTO tbl_SurgerySlotDescription
SELECT DISTINCT @.practiceCode, ssi.Label, ssi.AutoFillMessage, ssi.IsBookable, null
FROM tbl_SurgerySlotIn ssi
WHERE SUBSTRING(ssi.SurgerySlotKey,PATINDEX('%.%',ssi.Su rgerySlotKey)+1,5) = @.practiceCode
AND ssi.Label NOT IN (
SELECT Label
FROM tbl_SurgerySlotDescription
WHERE PracticeCode = @.practiceCode
)

-- insert data into proper tables with extract date added
INSERT INTO tbl_Surgery
SELECT
SurgeryKey,
GETDATE(),
ClinicianCode,
StartTime,
SessionGroup,
[Description],
SurgeryName,
Deleted,
PremisesKey,
@.practiceCode --SUBSTRING(SurgeryKey,PATINDEX('%.%',SurgeryKey)+1, 5)
FROM tbl_SurgeryIn

INSERT INTO tbl_SurgerySlot
SELECT
SurgerySlotKey,
GETDATE(),
SurgeryKey,
Length,
Deleted,
StartTime,
RestrictionDays,
Label,
IsRestricted,
@.practiceCode
FROM tbl_SurgerySlotIn

INSERT INTO tbl_Appointment
SELECT
AppointmentKey,
GETDATE(),
SurgerySlotKey,
PatientKey,
Cancelled,
Continuation,
Deleted,
Reason,
DateMade
FROM tbl_AppointmentIn

-- empty input tables
DELETE FROM tbl_SurgeryIn
DELETE FROM tbl_SurgerySlotIn
DELETE FROM tbl_AppointmentIn

FETCH NEXT FROM allPractices INTO @.practiceCode
END

CLOSE allPractices
DEALLOCATE allPractices

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Monday, March 19, 2012

Email Notifications

Hi All,
I have a sql2k running on w2k adv. svr and have a stored procedure which
sends email notifications to clients using xp_sendmail.
Out of the 25 email notifications 1 or 2 fails every other day which is
unacceptable to my clients.
Checking all my logs, I see that the xp_sendmail query runs successfully all
the time.
I need to understand why the query runs successfully all the time and yet
get 1 or 2 email failures?
ThanksIt can a problem with the mail system. Since SQL Server using MAPI to send a
mail, you should check your mail transport.
There are a lot of issues can happen during mail delivery. First of all try
to check is there everything is Ok with
your MS Outlook during the failure time. Then you can check MS Exchange
Server logs if you are using it.
It also can be network issue.
Regards
---
All information provided above AS IS.
"caddo65590" <caddo65590@.hotmail.com> wrote in message
news:%23tKMf4JpDHA.1096@.TK2MSFTNGP11.phx.gbl...
> Hi All,
> I have a sql2k running on w2k adv. svr and have a stored procedure which
> sends email notifications to clients using xp_sendmail.
> Out of the 25 email notifications 1 or 2 fails every other day which is
> unacceptable to my clients.
> Checking all my logs, I see that the xp_sendmail query runs successfully
all
> the time.
> I need to understand why the query runs successfully all the time and yet
> get 1 or 2 email failures?
> Thanks
>|||Thanks Sky,
I also forgot to add the error generated by sql, it might help.
The message below is generated anytime the email fails.
Msg 18025, Sev 16: xp_sendmail: failed with mail error 0x80004005
"SkyWalker" <tcp_43@.hotmail.com_TAKETHISOFF> wrote in message
news:Or72cUKpDHA.2444@.TK2MSFTNGP09.phx.gbl...
> It can a problem with the mail system. Since SQL Server using MAPI to send
a
> mail, you should check your mail transport.
> There are a lot of issues can happen during mail delivery. First of all
try
> to check is there everything is Ok with
> your MS Outlook during the failure time. Then you can check MS Exchange
> Server logs if you are using it.
> It also can be network issue.
>
> Regards
> ---
> All information provided above AS IS.
>
> "caddo65590" <caddo65590@.hotmail.com> wrote in message
> news:%23tKMf4JpDHA.1096@.TK2MSFTNGP11.phx.gbl...
> > Hi All,
> > I have a sql2k running on w2k adv. svr and have a stored procedure which
> > sends email notifications to clients using xp_sendmail.
> > Out of the 25 email notifications 1 or 2 fails every other day which is
> > unacceptable to my clients.
> > Checking all my logs, I see that the xp_sendmail query runs successfully
> all
> > the time.
> > I need to understand why the query runs successfully all the time and
yet
> > get 1 or 2 email failures?
> > Thanks
> >
> >
>|||Did you search KB for that return code. I found at least two articles for
that return code that referred to xp_sendmail.
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"caddo65590" <caddo65590@.hotmail.com> wrote in message
news:OKuLYdKpDHA.2776@.tk2msftngp13.phx.gbl...
> Thanks Sky,
> I also forgot to add the error generated by sql, it might help.
> The message below is generated anytime the email fails.
> Msg 18025, Sev 16: xp_sendmail: failed with mail error 0x80004005
> "SkyWalker" <tcp_43@.hotmail.com_TAKETHISOFF> wrote in message
> news:Or72cUKpDHA.2444@.TK2MSFTNGP09.phx.gbl...
> > It can a problem with the mail system. Since SQL Server using MAPI to
send
> a
> > mail, you should check your mail transport.
> > There are a lot of issues can happen during mail delivery. First of all
> try
> > to check is there everything is Ok with
> > your MS Outlook during the failure time. Then you can check MS Exchange
> > Server logs if you are using it.
> > It also can be network issue.
> >
> >
> >
> > Regards
> > ---
> > All information provided above AS IS.
> >
> >
> > "caddo65590" <caddo65590@.hotmail.com> wrote in message
> > news:%23tKMf4JpDHA.1096@.TK2MSFTNGP11.phx.gbl...
> > > Hi All,
> > > I have a sql2k running on w2k adv. svr and have a stored procedure
which
> > > sends email notifications to clients using xp_sendmail.
> > > Out of the 25 email notifications 1 or 2 fails every other day which
is
> > > unacceptable to my clients.
> > > Checking all my logs, I see that the xp_sendmail query runs
successfully
> > all
> > > the time.
> > > I need to understand why the query runs successfully all the time and
> yet
> > > get 1 or 2 email failures?
> > > Thanks
> > >
> > >
> >
> >
>

Email ID Validation in SQL Query

Hi,
Can someone help me with a query to validate if a value stored in the
emailaddress field in a table is a valid email id or no. Please mail me
ASAP. Thanks in advance.
Regards
DineshDinesh
I'm not sure iunderstood your question.
Since you have not provided a table structure along with sample data , I
guess you can do something like that
IF EXISTS (SELECT * FROM Table WHERE email_id=@.par)
--do soemthing
ELSE
--do somethimg else
"Dinesh" <Dinesh@.discussions.microsoft.com> wrote in message
news:80CE816E-A91C-4ECE-93E4-19006167B567@.microsoft.com...
> Hi,
> Can someone help me with a query to validate if a value stored in the
> emailaddress field in a table is a valid email id or no. Please mail me
> ASAP. Thanks in advance.
> Regards
> Dinesh|||Dinesh skrev:

> Hi,
> Can someone help me with a query to validate if a value stored in the
> emailaddress field in a table is a valid email id or no. Please mail me
> ASAP. Thanks in advance.
> Regards
> Dinesh
Depends on what you actually want to validate...
Is it only that the value looks like 'somename@.someaddress', or is it
to check if there really IS such an (active?) email account, or is it
to check against a table with valid email addresses? Or something else?
/impslayer, aka Birger Johansson|||Please explain in detail, your question? Mean provide sample data situation
Thanks,
Siva
"impslayer" wrote:

> Dinesh skrev:
>
> Depends on what you actually want to validate...
> Is it only that the value looks like 'somename@.someaddress', or is it
> to check if there really IS such an (active?) email account, or is it
> to check against a table with valid email addresses? Or something else?
> /impslayer, aka Birger Johansson
>

email from stored procedure

i want to know how can i send emails from SQL stored procedure? is it possible?

Yes.

If you are using SQL 2000, you should investigate xp_smtpmail. Check these sources:

http://www.sqldev.net/xp/xpsmtp.htm
http://www.aspfaq.com/show.asp?id=2403

If you are using SQL 2005, refer to Books Online, Topic: 'Database Mail'

|||

Hi Jassim,

I useed this script for SQL 2000. Take care of the parameters inside the procedure, such as server name or user account.

Code Snippet

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[spSendMail]

@.From varchar(150) ,

@.To varchar(150) ,

@.Bcc varchar(500) = null,

@.Subject varchar(400)=" ",

@.Body ntext =" "

--WITH ENCRYPTION

AS

Declare @.object int

Declare @.hr int

EXEC @.hr = sp_OACreate 'CDO.Message', @.object OUT

EXEC @.hr = sp_OASetProperty @.object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'

EXEC @.hr = sp_OASetProperty @.object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', 'mailserver.domain.com'

--BodyFormat = cdoBodyFormatHTML

EXEC @.hr = sp_OASetProperty @.object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','1'

EXEC @.hr = sp_OASetProperty @.object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value','DOMAIN\user'

EXEC @.hr = sp_OASetProperty @.object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value','Pa$$w0rd'

EXEC @.hr = sp_OAMethod @.object, 'Configuration.Fields.Update', null

EXEC @.hr = sp_OASetProperty @.object, 'To', @.To

EXEC @.hr = sp_OASetProperty @.object, 'Bcc', @.Bcc

EXEC @.hr = sp_OASetProperty @.object, 'From', @.From

EXEC @.hr = sp_OASetProperty @.object, 'Subject', @.Subject

--use TextBody for plain text

EXEC @.hr = sp_OASetProperty @.object, 'HTMLBody', @.Body

EXEC @.hr = sp_OAMethod @.object, 'Send', NULL

--?

IF @.hr <> 0

BEGIN

EXEC sp_OAGetErrorInfo @.object

RETURN @.object

END

PRINT 'success'

EXEC @.hr = sp_OADestroy @.object

GO

and also you can use Arnie's links or in case of SQL 2005, use sp_send_dbmail.

Regards,

Janos

Email From a CLR Stored Proc - SMTPPermission

I am trying to send email from a CLR Stored proc.

I get the following error.

A .NET Framework error occurred during execution of user defined routine or aggregate 'HelloWorld':
System.Security.SecurityException:

Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.Mail.SmtpClient.Initialize()
at System.Net.Mail.SmtpClient..ctor(String host)
at StoredProcedures.HelloWorld()
.

How do I handle the SmtpPermission?

Here is the stored proc

public static void HelloWorld()
{
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("test@.test.EDU");
mail.To.Add("test@.test.EDU");

//set the content
mail.Subject = "Hello World";
mail.Body = "Did you get this? I am emailing from a CLR stored proc!";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

}

SQL Server comes with when last I counted three different mail internal so you are reinventing the wheel by using System.Net. Run a search for SQL Server mail and SQL Server Agent mail in the BOL (books online) because I think there are known issues with the IMAPI mail. SQL Server Agent mail can also be used to send pages. Hope this helps.|||

Have the same problen - and the above RUDE comment isnt any help!

Instantiating SmtpClient gives -exception of type 'System.Security.SecurityException'

Hmmm - if it would work we've got a lot of objects we could put to work without reenventing new methods just to work in a CLR proc!

What's necessary to get SmtpClient to work in a CLR proc?

|||

OK - there's 2 or 3 ways to get the job done...

Easiest( although not MSDN recommended just to jet a CLR proc to run) is to set the permission level to External_Access...

SQL Server Host Policy Level Permission Sets
The set of code access security permissions granted to assemblies by the SQL Server host policy level is determined by the permission set specified when creating the assembly. There are three permission sets:SAFE,EXTERNAL_ACCESS andUNSAFE.

The permision level is set on the properties pages of the CLR project , database tab - set Permission Level-External, set Aassembly Owner-dbo, and run tsql 'ALTERDATABASE DataBaseName SET TRUSTWORTHYON'
This will get the job DONE! - and the SmtpClient wiill work ok...

Then do it right and Sign the Assenbly with a Strong name Key file...
Read MSDN

Creating an Assembly

Discusses creating SAFE, EXTERNAL_ACCESS, and UNSAFE CLR assemblies in SQL Server

That's it...

RLewis - MCSD

(and there sure is a lot of NO HELP answers goin around...)

|||

Brilliant!!!!!

Solution 2 worked like a charm. El mucho gracias!!!!

Party!!!

Friday, March 9, 2012

Email address in stored Procedure parameters

Hi, i'm writing a simple sp that check if an email address already exist in
an AddressBook table.
This sp expect a parameters called @.Address.
When i call this sp and pass to it an email address which contains an at
character (@.) the stored procedure will return no values.
i think the @. alter the parameters value.
Whitout the @. char the sp works fine .
Can someone help me?
ThanksIf you post the stored procedure the chances of getting help will be
much, much better.
Roy Harvey
Beacon Falls, CT
On Wed, 3 May 2006 14:59:01 -0700, Pantano Antonio
<PantanoAntonio@.discussions.microsoft.com> wrote:

>Hi, i'm writing a simple sp that check if an email address already exist in
>an AddressBook table.
>This sp expect a parameters called @.Address.
>When i call this sp and pass to it an email address which contains an at
>character (@.) the stored procedure will return no values.
>i think the @. alter the parameters value.
>Whitout the @. char the sp works fine .
>Can someone help me?
>Thanks|||How does your routine differ from the following?
create table address_book
(
address varchar(64)
)
insert address_book values ('address@.google.com')
create procedure add_check
@.address varchar(64)
as
if exists (select 1 from address_book where address = @.address)
print 'exists'
else
print 'not exists'
declare @.address varchar(64)
set @.address = 'address@.google.com'
exec add_check @.address|||Thanks. I solved. The error was that the parameter i declared was nvarchar
without the size.
When i changed it whit the same size of the table column, nvarchar(70), the
stored procedure works well and return me the right result.
Thanks to all.

Sunday, February 26, 2012

ELSEIF in Stored Procedures?

I have found some documentation regarding the use of IF...ELSE statements in stored procedures, but what about multiple condition statements? For example, say I need 3 unique fields in my table. If my application passes a value that is a duplicate in one of the columns, the stored proc will fail, but it is difficult to know which item caused the failure and therefore difficult for the user to get a meaningful error message in order to correct their input. I am thinking I could just make a conditional statement that applies a code to an OUTPUT parameter in order to clarify the error:

(pseudo code)

if @.field1 already exists then @.output = '1';terminate stored procedure

elseif @.field2 already exists then @.output = '2';terminate stored procedure

elseif @.field3 already exists then @.output = '3';terminate stored procedure

else finish the insert

(end pseudo code)

Are 'elseif' statements allowed in SQL Server? Am I going about this in the wrong way?

Jungalist wrote:

Are 'elseif' statements allowed in SQL Server?

Sort of. You can impletemt the logic as follows :

IF @.field1 already exists

BEGIN

SET @.OUTPUT = 1

END

ELSE

IF @.field2 already exists

BEGIN

SET @.OUTPUT=2

END

ELSE

IF @.field3 already exists

BEGIN

SET @.OUTPUT=3

END

check out books on line for "IF ELSE"

|||

Thank-you. I was searching for the wrong terms. I appreciate the help.

Else statement is not working in the stored procedure

I have a stored procedure that needs to populate the fields of records for tblBag_data from the tblshipping_sched if not found it looks in the tblshipment_history. But it not checking the history table(2nd table). Please help!

CREATE Procedure spUpdate_bag_data
@.t1 int OUT
AS

declare @.work_ord_num char(9), @.two char(7), @.work_ord_line_num char(3), @.cust_num char(5), @.cust_name char(50), @.apple_part_num char(12), @.apple_catalog_num char(28);

Declare update_bag CURSOR
FOR
SELECT work_ord_num, work_ord_line_num
FROM tblBag_data
WHERE cust_num IS NULL;

OPEN update_bag
FETCH NEXT FROM update_bag INTO @.work_ord_num, @.work_ord_line_num

WHILE @.@.FETCH_STATUS = 0 --and @.counter<30
BEGIN
--set @.counter = @.counter + 1
SET @.two = LEFT(@.work_ord_num,6) + '%'
set @.cust_num = '';

SELECT @.cust_num = cust_num, @.cust_name = cust_name, @.apple_part_num = apple_part_num, @.apple_catalog_num = apple_catalog_num
FROM tblShipping_sched
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;



IF @.@.RowCount > 0
BEGIN

UPDATE tblBag_data
SET cust_num = @.cust_num, cust_name = @.cust_name, apple_part_num = @.apple_part_num, apple_catalog_num = @.apple_catalog_num
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;
END

ELSE
BEGIN

SELECT cust_num = @.cust_num, cust_name =@.cust_name, apple_part_num =@.apple_part_num, apple_catalog_num = @.apple_catalog_num FROM tblShipment_history
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;

IF @.cust_num IS NOT NULL and len(@.cust_num)= 5
UPDATE tblBag_data SET cust_num = @.cust_num, cust_name = @.cust_name, apple_part_num = @.apple_part_num, apple_catalog_num = @.apple_catalog_num
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;

END

FETCH NEXT FROM update_bag INTO @.work_ord_num, @.work_ord_line_num
END

close update_bag
deallocate update_bag

return(1)Why are you using a cursor? There's no need.

Also if
work_ord_num AND wrk_ord_line_num

are not the primary or a unique constraint to
FROM tblShipping_sched

Then you can get back multiple rows...and your assingment to the variables will be the last one returned...

And since there is no input variable to the sproc, it means you'll be doing every row in the table every time you run it...
well where cust_num is null

Need to see the DDL for the three tables...(sample data wouldn't hurt either)|||Originally posted by Brett Kaiser
Why are you using a cursor? There's no need.

Also if
work_ord_num AND wrk_ord_line_num

are not the primary or a unique constraint to
FROM tblShipping_sched

Then you can get back multiple rows...and your assingment to the variables will be the last one returned...

And since there is no input variable to the sproc, it means you'll be doing every row in the table every time you run it...
well where cust_num is null

Need to see the DDL for the three tables...(sample data wouldn't hurt either)

Hi Brett,

I have a tblBag_data that needs four fields populated from the tblshippping_sched or tblShipment_history. The stored procedure is takes the work_ord_num and work_ord_line_num in tblBag_data and match them to the tblshipping_sched if the cust_num is null. It loops thru the tblshipping_sched for that record if it finds the record it populate the four fields(cust_name, cust_num..)in the tblBag_data. But if it doesn't find it it suppose to go to tblShipment_history table and loops thru for the same record and populates the tblBag_data once it finds it.

The If statement seems to be working fine. But else is definitely not working. If there is better way to write this without cursor please provide some sample code.

Thank you.
I hope it|||Sorry...work got in the way...

How about:

UPDATE l
SET cust_num = r.cust_num
, cust_name = r.cust_name
, apple_part_num = r.apple_part_num
, apple_catalog_num = r.apple_catalog_num
FROM tblBagData l
INNER JOIN tblBagData r
ON r.work_ord_num like LEFT(l.work_ord_num,6) + '%'
AND r.work_ord_line_num = l.work_ord_line_num
WHERE cust_num IS NULL

And you don't even have to worry if it finds it ot not because youcan then just do the second query, because the cust_num will still be null|||Originally posted by Brett Kaiser
Sorry...work got in the way...

How about:

UPDATE l
SET cust_num = r.cust_num
, cust_name = r.cust_name
, apple_part_num = r.apple_part_num
, apple_catalog_num = r.apple_catalog_num
FROM tblBagData l
INNER JOIN tblBagData r
ON r.work_ord_num like LEFT(l.work_ord_num,6) + '%'
AND r.work_ord_line_num = l.work_ord_line_num
WHERE cust_num IS NULL

And you don't even have to worry if it finds it ot not because youcan then just do the second query, because the cust_num will still be null

Hmm... This might be a solution.
I'll give it a try.
Thanks!

Eliminating Dynamic SQL

I am refactoring stored procedures that use dynamic sql.
The reason the store procedures use dynamic sql is because
the data that is need comes from another MS SQL database
that resides on the same server instance.
The following is a code example from a stored procedure:
declare @.theDatabase sysname;
set @.theDatabase = 'SomeDatabase';
declare @.thePrimaryKey int;
set @.thePrimaryKey = 2;
declare @.theSqlString varchar(8000);
set @.theSqlString = 'select r.Feild from ' +
@.theDatabase +
'..TableName r ' +
'where r.ID = '
+ rtrim( str( @.thePrimaryKey ) )
exec sp_executesql @.theSqlString;
The problem is that dynamic sql is EXTREMELY slow! I have
been doing some research and have found a little bit on
sp_linkedservers and OPENQUERY but I have not figured out
how to accomplish what is done above.
Can anyone give me an example of how I can use
sp_linkedservers and OPENQUERY to accomplish this query?
Would it be better, performance wise, to just continue
using dynamic sql?
Any help would be greatly appreciated.
Sincerely,
John Dickey
jpd0861@.msn.comJohn Dickey wrote:
> I am refactoring stored procedures that use dynamic sql.
> The reason the store procedures use dynamic sql is because
> the data that is need comes from another MS SQL database
> that resides on the same server instance.
> The following is a code example from a stored procedure:
> declare @.theDatabase sysname;
> set @.theDatabase = 'SomeDatabase';
> declare @.thePrimaryKey int;
> set @.thePrimaryKey = 2;
> declare @.theSqlString varchar(8000);
> set @.theSqlString = 'select r.Feild from ' +
> @.theDatabase +
> '..TableName r ' +
> 'where r.ID = '
> + rtrim( str( @.thePrimaryKey ) )
> exec sp_executesql @.theSqlString;
> The problem is that dynamic sql is EXTREMELY slow! I have
> been doing some research and have found a little bit on
> sp_linkedservers and OPENQUERY but I have not figured out
> how to accomplish what is done above.
> Can anyone give me an example of how I can use
> sp_linkedservers and OPENQUERY to accomplish this query?
> Would it be better, performance wise, to just continue
> using dynamic sql?
If you code the dynamic SQL properly, you can eliminate much of the
performance overhead. In your case, you're using sp_executesql, but are
not taking advantage of its main benefit over EXEC. That is, parameter
binding.
Instead of how it's currently coded, you could use:
set @.theSqlString = 'select r.Feild from ' +
'[' + @.theDatabase + ']' +
'..TableName r ' +
'where r.ID = @.thePrimaryKey'
Exec sp_executesql @.theSqlString, N'thePrimaryKey INT', @.thePrimaryKey
I'm not sure why it's necessary, though, given your example. You have a
hard-coded database name on the same server. If you know the database,
you can just fully-qualify the object in the SQL:
Select * from [AnotherDatabase].dbo.[MyTable]
David Gugick
Imceda Software
www.imceda.com|||Why do you want to parameterize the name of the target database?
If the name may change and/or you prefer not to hard-code it in SPs
then just create a view or views to reference the other database and
reference only the views in your SPs. That way the database name is
coded only in a few places instead of every SP.
David Portas
SQL Server MVP
--

Eliminating Dynamic SQL

I am refactoring stored procedures that use dynamic sql.
The reason the store procedures use dynamic sql is because
the data that is need comes from another MS SQL database
that resides on the same server instance.
The following is a code example from a stored procedure:
declare @.theDatabase sysname;
set @.theDatabase = 'A database that I get based on
critera at runtime.';
declare @.thePrimaryKey int;
set @.thePrimaryKey = 2;
declare @.theSqlString varchar(8000);
set @.theSqlString = 'select r.Feild from ' +
@.theDatabase +
'..TableName r ' +
'where r.ID = '
+ rtrim( str( @.thePrimaryKey ) )
exec sp_executesql @.theSqlString;
The problem is that dynamic sql is EXTREMELY slow! I have
been doing some research and have found a little bit on
sp_linkedservers and OPENQUERY but I have not figured out
how to accomplish what is done above.
Can anyone give me an example of how I can use
sp_linkedservers and OPENQUERY to accomplish this query?
Would it be better, performance wise, to just continue
using dynamic sql?
Any help would be greatly appreciated.
Sincerely,
John Dickey
jpd0861@.msn.comIf you have the same table structures in each database then you can
create a partitioned view across them and reference the partitioned
view instead. Whether this is the right solution though may depend on
just why you have the data split across multiple DBs in the first
place. It doesn't seem like a very practical architecture if it forces
you to write dynamic SQL in all your production code.
David Portas
SQL Server MVP
--|||Thank you for your reply David.
The reason that we have different databases is because the
databases are for different applications that my
application interfaces with. They happen to be Great Plains
accounting databases that I am trying to retrive data from.
Can you give me more information about the partitioned view
and how I can set that up?
John Dickey

>--Original Message--
>If you have the same table structures in each database
then you can
>create a partitioned view across them and reference the
partitioned
>view instead. Whether this is the right solution though
may depend on
>just why you have the data split across multiple DBs in
the first
>place. It doesn't seem like a very practical architecture
if it forces
>you to write dynamic SQL in all your production code.
>--
>David Portas
>SQL Server MVP
>--
>.
>|||It's all in Books Online:
http://msdn.microsoft.com/library/e...des_06_17zr.asp
David Portas
SQL Server MVP
--

Friday, February 24, 2012

Eliminate Duplicate ID's in this StoredProcedure

I have a stored procedure that I use for Monthly Billing

delete from BillingCurrent

insert into BillingCurrent([Name],Address,City,State,Zip,InvoiceID,CustomerID,[Date],InvoiceTotal)

SELECT Customers.Name,Customers.Address,Customers.City,Customers.State,Customers.Zip,Invoices.InvoiceID,Customers.CustomerID,Invoices.Date,Invoices.InvoiceTotal

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-30,GETDATE()), 112)and CONVERT(varchar(15), GETDATE(), 112)

This works great, but if a customer has more than one invoice open it adds that Customer again (for each invoice that is not 0.00. How can I change this SP to only add each Customer once regardless of how many invoices they have?

In your query you tried to work with Invoice.Date and Invoice.InvoiceTotal fields.

If you have following data in your Invoices table for CustomerId=1:

Date InvoiceTotal

1.1.2006 1000

1.1.2007 2000

What do you want to save into BillingCurrent?

Which SQL Server version do you use?

|||

Cammyr:

You state that you want to reduce the number of rows to 1 whenever a customer has more than one invoice during a month. However, what I don't understand is what we are supposed to put in these three fields if we are returning only one row: (1) InvoiceID, (2) Date, and (3) InvoiceTotal. I have therefore put together two methods to return only one row per customer. The first method simply returns this information for the last invoice posted for a given customer. There is a good chance that this is not what you want but would rather have the total of all invoices summed into the InvoiceTotal column:

--
-- This method uses uses (1) CROSS APPLY and (2) ROW_NUMBER() to
-- find the "last" invoice posted to a particular customers account to
-- display that information with the customer information.
--

SELECT c.Name,
c.Address,
c.City,
c.State,
c.Zip,
i.InvoiceID,
c.CustomerID,
i.Date,
i.InvoiceTotal
FROM Customers c
cross apply
( select invoiceId,
row_number ()
over ( order by Date desc,
invoiceId desc
)
as seq,
Date,
InvoiceTotal
from Invoices p
where p.customerId = c.customerId
and CONVERT(varchar(15), p.Date, 112)
Between CONVERT(varchar(15),dateadd (d,-30,GETDATE()), 112)
and CONVERT(varchar(15), GETDATE(), 112)
) i
where i.seq = 1

-- Name Address City State Zip InvoiceID CustomerID Date InvoiceTotal
-- -- -- -- -- --
-- Dave Mugambo 1318 Mockingbird Lane Munster In 46321 4 1 2007-02-28 00:00:00.000 14.92


--
-- This method instead posts the SUM of all invoices into the InvoiceTotal
-- column and chooses the highest InvoiceID for the InvoiceID column and
-- the highest Date for the Date column. It is not clear what is requried
-- for these fields
--

SELECT c.Name,
c.Address,
c.City,
c.State,
c.Zip,
max (i.InvoiceID) as InvoiceID,
c.CustomerID,
max (i.Date) as Date,
sum (i.InvoiceTotal) as InvoiceTotal
FROM Invoices i
INNER JOIN Customers c
ON i.CustomerID = c.CustomerID
WHERE CONVERT(varchar(15), i.Date, 112)
Between CONVERT(varchar(15),dateadd (d,-30,GETDATE()), 112)
and CONVERT(varchar(15), GETDATE(), 112)
group by c.CustomerId,
c.Name,
c.Address,
c.City,
c.State,
c.Zip

-- Name Address City State Zip InvoiceID CustomerID Date InvoiceTotal
-- -- -- -- -- --
-- Dave Mugambo 1318 Mockingbird Lane Munster In 46321 4 1 2007-02-28 00:00:00.000 94.76

( I completely agree with Konstantin's questions. )

|||

I want to save Name,Address,City,State,Zip,InvoiceID,CustomerID in BillingCurrent

But really I guess I don't need InvoiceID or InvoiceTotal as I have a relationship between BillingCurrent and InvoiceDetails On CustomerID Column that will get all the InvoiceDetails for each CustomerID.

I'm using vb.express with sql.express

|||

You are both right there were quite a few fields in my insert statement that were not needed. All I really needed was CustomerID,Name,Address,City and Zip because I can use the getchildrows method to get all of the corresponding invoicedetails for each customerID.

This is what I came up with, it seems to be working fine.

insert into BillingCurrent (CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE (CONVERT(varchar(15), Invoices.Date, 112) BETWEEN CONVERT(varchar(15), DATEADD(d, - 30, GETDATE()), 112) AND CONVERT(varchar(15),

GETDATE(), 112))

I'm sure yours works for that situation Kent, so I'm marking yours as the answer.

Thanks

Sunday, February 19, 2012

Efficient Date Query?

Have data stored in two separate fields (Start_Date and End_Date). i.e.:
ID Start_Date End_Date
1 5/5/2006 7/8/2006
2 7/7/2006 7/9/2006
3 6/8/2006 8/5/2006
....
Basically I have to query this based on a MONTH and a YEAR and return all
records where any day in that month will fall between the start/end date.
So if I pass 7/2006 to the query, it will return all of the records. If I
choose 8/2006 it will only return the last one. If I choose 6/2006 it will
return the first and last. Theoretically, I could do a UNION of between
statements for every day a given month, but that is ridiculously ineffecient
and borderline stupid. There has to be a good way of doing this, I just
can't think of it off the top of my head and the guy who's paid to do so
isn't here. Can someon refresh my memory on an easy way to do this?
Thank you!This is a multi-part message in MIME format.
--=_NextPart_000_081B_01C6A751.D81740F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
The guy that's paid to do this isn't here either...
This may work for you. (I've selected using the ISO format for date.)
DECLARE @.MyDate varchar(6)
DECLARE @.Durations table
( ID int
, Start_Date datetime
, End_Date datetime
)
INSERT INTO @.Durations Values (1, '5/5/2006','7/8/2006')
INSERT INTO @.Durations Values (2, '7/7/2006','7/9/2006')
INSERT INTO @.Durations Values (3, '6/8/2006','8/5/2006')
SET @.MyDate =3D '200608'
SELECT ID
, Start_Date
, End_Date
FROM @.Durations WHERE @.MyDate BETWEEN ( convert( varchar(6), Start_Date, 112 )) AND ( =convert( varchar(6), End_Date, 112 ))
-- Arnie Rowland
"To be successful, your heart must accompany your knowledge."
"James" <minorkeys@.gmail.com> wrote in message =news:uP8Lwh4pGHA.516@.TK2MSFTNGP05.phx.gbl...
> Have data stored in two separate fields (Start_Date and End_Date). =i.e.:
> > ID Start_Date End_Date
> 1 5/5/2006 7/8/2006
> 2 7/7/2006 7/9/2006
> 3 6/8/2006 8/5/2006
> ....
> > Basically I have to query this based on a MONTH and a YEAR and return =all > records where any day in that month will fall between the start/end =date. > So if I pass 7/2006 to the query, it will return all of the records. =If I > choose 8/2006 it will only return the last one. If I choose 6/2006 it =will > return the first and last. Theoretically, I could do a UNION of =between > statements for every day a given month, but that is ridiculously =ineffecient > and borderline stupid. There has to be a good way of doing this, I =just > can't think of it off the top of my head and the guy who's paid to do =so > isn't here. Can someon refresh my memory on an easy way to do this?
> > Thank you! > >
--=_NextPart_000_081B_01C6A751.D81740F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

The guy that's paid to do this isn't =here either...
This may work for you. (I've selected =using the ISO format for date.)
DECLARE @.MyDate =varchar(6)
DECLARE @.Durations =table ( ID int , Start_Date datetime , End_Date datetime )
INSERT INTO @.Durations Values =(1, '5/5/2006','7/8/2006')INSERT INTO @.Durations Values (2, '7/7/2006','7/9/2006')INSERT INTO @.Durations Values (3, '6/8/2006','8/5/2006')
SET @.MyDate =3D ='200608'
SELECT = ID , Start_Date , End_DateFROM =@.Durations WHERE @.MyDate BETWEEN ( convert( varchar(6), Start_Date, 112 )) AND =( convert( varchar(6), End_Date, 112 ))
-- Arnie Rowland"To be =successful, your heart must accompany your knowledge."
"James" =wrote in message news:uP8Lwh4pGHA.516@.TK2MSFTNGP05.phx.gbl...> =Have data stored in two separate fields (Start_Date and End_Date). i.e.:> => ID Start_Date End_Date> 1 5/5/2006 7/8/2006> 2 7/7/2006 7/9/2006> 3 6/8/2006 8/5/2006> ....> > Basically I have to query this based on a MONTH =and a YEAR and return all > records where any day in that month will =fall between the start/end date. > So if I pass 7/2006 to the query, =it will return all of the records. If I > choose 8/2006 it will =only return the last one. If I choose 6/2006 it will > return the first =and last. Theoretically, I could do a UNION of between > =statements for every day a given month, but that is ridiculously ineffecient > =and borderline stupid. There has to be a good way of doing this, I =just > can't think of it off the top of my head and the guy who's paid =to do so > isn't here. Can someon refresh my memory on an easy =way to do this?> > Thank you! > >

--=_NextPart_000_081B_01C6A751.D81740F0--

Efficient Date Query?

Have data stored in two separate fields (Start_Date and End_Date). i.e.:
ID Start_Date End_Date
1 5/5/2006 7/8/2006
2 7/7/2006 7/9/2006
3 6/8/2006 8/5/2006
....
Basically I have to query this based on a MONTH and a YEAR and return all
records where any day in that month will fall between the start/end date.
So if I pass 7/2006 to the query, it will return all of the records. If I
choose 8/2006 it will only return the last one. If I choose 6/2006 it will
return the first and last. Theoretically, I could do a UNION of between
statements for every day a given month, but that is ridiculously ineffecient
and borderline stupid. There has to be a good way of doing this, I just
can't think of it off the top of my head and the guy who's paid to do so
isn't here. Can someon refresh my memory on an easy way to do this?
Thank you!The guy that's paid to do this isn't here either...
This may work for you. (I've selected using the ISO format for date.)
DECLARE @.MyDate varchar(6)
DECLARE @.Durations table
( ID int
, Start_Date datetime
, End_Date datetime
)
INSERT INTO @.Durations Values (1, '5/5/2006','7/8/2006')
INSERT INTO @.Durations Values (2, '7/7/2006','7/9/2006')
INSERT INTO @.Durations Values (3, '6/8/2006','8/5/2006')
SET @.MyDate = '200608'
SELECT
ID
, Start_Date
, End_Date
FROM @.Durations
WHERE @.MyDate BETWEEN ( convert( varchar(6), Start_Date, 112 )) AND ( conver
t( varchar(6), End_Date, 112 ))
--
Arnie Rowland
"To be successful, your heart must accompany your knowledge."
"James" <minorkeys@.gmail.com> wrote in message news:uP8Lwh4pGHA.516@.TK2MSFTNGP05.phx.gbl...[
vbcol=seagreen]
> Have data stored in two separate fields (Start_Date and End_Date). i.e.:
>
> ID Start_Date End_Date
> 1 5/5/2006 7/8/2006
> 2 7/7/2006 7/9/2006
> 3 6/8/2006 8/5/2006
> ....
>
> Basically I have to query this based on a MONTH and a YEAR and return all
> records where any day in that month will fall between the start/end date.
> So if I pass 7/2006 to the query, it will return all of the records. If I
> choose 8/2006 it will only return the last one. If I choose 6/2006 it wil
l
> return the first and last. Theoretically, I could do a UNION of between
> statements for every day a given month, but that is ridiculously ineffecie
nt
> and borderline stupid. There has to be a good way of doing this, I just
> can't think of it off the top of my head and the guy who's paid to do so
> isn't here. Can someon refresh my memory on an easy way to do this?
>
> Thank you!
>
>[/vbcol]

Friday, February 17, 2012

Effects of changing a table name

If I rename a table on my SQL Server 2005 database say from Carriers to
Partners, will it update the stored procedures that reference the old table
name "carriers" to the new "partners" name? I am talking about using the
rename function in the rename function in the micosoft SQL Server managment
studio program. Or will I have to edit my 1,351 stored procedures manually?
thanks!> If I rename a table on my SQL Server 2005 database say from Carriers to
> Partners, will it update the stored procedures that reference the old
> table name "carriers" to the new "partners" name?
No, SQL Server is not going to go and edit your code for you. It would be
hard enough to find all the places it is referenced in vanilla T-SQL (and
this would rely on a correct sysdepends, rather than deferred name
resolution), what about all the places where the table name could be
referenced dynamically (e.g. EXEC('SELECT * INTO t1 FROM Carriers', or
EXEC('SELECT * INTO t1 FROM Car'+'riers'), or EXEC('SELECT * FROM
'+@.TableName))? Even if you took your procedure code offline and did a
grep/replace, you're still not guaranteed to find every single location.

> I am talking about using the rename function in the rename function in the
> micosoft SQL Server managment studio program. Or will I have to edit my
> 1,351 stored procedures manually?
An alternative would be to create a synonym.
A

Wednesday, February 15, 2012

Effect of a SELECT Stored Procedure on tables or database

Hi all,

I have a few stored procedures which all perfom SELECT queries on a table in the database. Do these kind of stored procedures affect any other processes or procedures working on that table. I am talking about locks, blocks etc.

For example, the database has a table which gets updated periodically by some process which I don't know. Now I wrote some stored procedures just to do the SQL SELECT with some conditions in WHERE clause. Is there any possibility that my stored procedure failed and the because of this, the process that runs on the table was not executed?

No, SELECT statements don't cause any kind of locking.|||

And if I don't talk about locking, can I be rest assured that there won't be anything else that can have an effect?

|||

Selects are pretty unobtrusive. I can't think of anything you'd need to worry about.

|||

gt1329a:

No, SELECT statements don't cause any kind of locking.

If your isolation level is read committed, SELECTs do put a shared resource lock but it doesnt block any UPDATEs. Locking is different from Blocking. If reading to-the-minute committed data is not important you can explicitly use NOLOCK.