SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Setting a variable using select with other variables in the select statement


Setting a variable using select with other variables in the select statement

Author
Message
bswhipp
bswhipp
SSC Veteran
SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)

Group: General Forum Members
Points: 213 Visits: 59
To all:. Happy Holidays. I need some help on this statement.

Currently this works but I need to change it to allow for the ability to change a database name.
@nextmonth is determined above and is set to the first day of the next month.

Declare @OpenCNFtotal numeric(15,5)
Set @OpenCNFtotal = (Select Coalesce(sum(BackOAmt), 0) as OCO
From M2MAux01..GetBackOrderDetails_View
where 1=1 and (fduedate < @nextmonth) and (Upper(fStatus) = 'OPEN') and (ShipdtCnfm = 1))

What I want to be able to do is change M2MAux01 to M2MAux + a number based on what database they are in.

I have tried but for some reason can't get the syntax correct. Any help would be much appreciated. Thanks

Declare @sql nvarchar(2500)
Declare @co char(2) = '01'

SET @sql = 'Select Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @nextmonth)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'

Exec sp_executesql
@query = @sql,
@params = N'@OpenCNFTotal Numeric output',
@OpenCNFtotalOUT = @OpenCNFtotal OUTPUT
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93801 Visits: 20670
bswhipp - Tuesday, December 26, 2017 6:07 AM
To all:. Happy Holidays. I need some help on this statement.

Currently this works but I need to change it to allow for the ability to change a database name.
@nextmonth is determined above and is set to the first day of the next month.

Declare @OpenCNFtotal numeric(15,5)
Set @OpenCNFtotal = (Select Coalesce(sum(BackOAmt), 0) as OCO
From M2MAux01..GetBackOrderDetails_View
where 1=1 and (fduedate < @nextmonth) and (Upper(fStatus) = 'OPEN') and (ShipdtCnfm = 1))

What I want to be able to do is change M2MAux01 to M2MAux + a number based on what database they are in.

I have tried but for some reason can't get the syntax correct. Any help would be much appreciated. Thanks

Declare @sql nvarchar(2500)
Declare @co char(2) = '01'

SET @sql = 'Select Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @nextmonth)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'

Exec sp_executesql
@query = @sql,
@params = N'@OpenCNFTotal Numeric output',
@OpenCNFtotalOUT = @OpenCNFtotal OUTPUT

First of all you are missing the specs of the numeric (highlighted above), secondly, simply add a USE [DATABASE_NAME] in front of the statement to execute it in another database.
Cool

bswhipp
bswhipp
SSC Veteran
SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)

Group: General Forum Members
Points: 213 Visits: 59
Eirikur Eiriksson - Tuesday, December 26, 2017 6:33 AM
bswhipp - Tuesday, December 26, 2017 6:07 AM
To all:. Happy Holidays. I need some help on this statement.

Currently this works but I need to change it to allow for the ability to change a database name.
@nextmonth is determined above and is set to the first day of the next month.

Declare @OpenCNFtotal numeric(15,5)
Set @OpenCNFtotal = (Select Coalesce(sum(BackOAmt), 0) as OCO
From M2MAux01..GetBackOrderDetails_View
where 1=1 and (fduedate < @nextmonth) and (Upper(fStatus) = 'OPEN') and (ShipdtCnfm = 1))

What I want to be able to do is change M2MAux01 to M2MAux + a number based on what database they are in.

I have tried but for some reason can't get the syntax correct. Any help would be much appreciated. Thanks

Declare @sql nvarchar(2500)
Declare @co char(2) = '01'

SET @sql = 'Select Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @nextmonth)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'

Exec sp_executesql
@query = @sql,
@params = N'@OpenCNFTotal Numeric output',
@OpenCNFtotalOUT = @OpenCNFtotal OUTPUT

First of all you are missing the specs of the numeric (highlighted above), secondly, simply add a USE [DATABASE_NAME] in front of the statement to execute it in another database.
Cool


Thanks for the quick reply. I added the specs but still get an error "The parameterized query '(@OpenCNFTotal Numeric(15,5) output)Select Coalesce(sum(BackOAmt' expects the parameter '@OpenCNFTotal', which was not supplied."

This in a report so I really don't want to change databases then change back if I don't have to. The report is run in the database or XXX01 or XXX02 and so on. Each XXX database has an alternate Auxiliary database such as XXXAUX01 goes with XXX01. and so on. The report is run from one central application and needs to be able to select Which XXXAUX database it needs depending on which database it is run from.
Thanks
bswhipp
bswhipp
SSC Veteran
SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)SSC Veteran (213 reputation)

Group: General Forum Members
Points: 213 Visits: 59
bswhipp - Tuesday, December 26, 2017 6:54 AM
Eirikur Eiriksson - Tuesday, December 26, 2017 6:33 AM
bswhipp - Tuesday, December 26, 2017 6:07 AM
To all:. Happy Holidays. I need some help on this statement.

Currently this works but I need to change it to allow for the ability to change a database name.
@nextmonth is determined above and is set to the first day of the next month.

Declare @OpenCNFtotal numeric(15,5)
Set @OpenCNFtotal = (Select Coalesce(sum(BackOAmt), 0) as OCO
From M2MAux01..GetBackOrderDetails_View
where 1=1 and (fduedate < @nextmonth) and (Upper(fStatus) = 'OPEN') and (ShipdtCnfm = 1))

What I want to be able to do is change M2MAux01 to M2MAux + a number based on what database they are in.

I have tried but for some reason can't get the syntax correct. Any help would be much appreciated. Thanks

Declare @sql nvarchar(2500)
Declare @co char(2) = '01'

SET @sql = 'Select Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @nextmonth)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'

Exec sp_executesql
@query = @sql,
@params = N'@OpenCNFTotal Numeric output',
@OpenCNFtotalOUT = @OpenCNFtotal OUTPUT

First of all you are missing the specs of the numeric (highlighted above), secondly, simply add a USE [DATABASE_NAME] in front of the statement to execute it in another database.
Cool


Thanks for the quick reply. I added the specs but still get an error "The parameterized query '(@OpenCNFTotal Numeric(15,5) output)Select Coalesce(sum(BackOAmt' expects the parameter '@OpenCNFTotal', which was not supplied."

This in a report so I really don't want to change databases then change back if I don't have to. The report is run in the database or XXX01 or XXX02 and so on. Each XXX database has an alternate Auxiliary database such as XXXAUX01 goes with XXX01. and so on. The report is run from one central application and needs to be able to select Which XXXAUX database it needs depending on which database it is run from.
Thanks


GOT IT!!

Declare @total Numeric(15,5)
Declare @sql nvarchar(2500)
Declare @param nvarchar(500)
Declare @co char(2) = '01'
declare @date datetime = Cast('01/01/2018' as datetime)
Set @sql = N'Select @totalOut = Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @date)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'
Set @param = N'@totalOUT numeric(15,2) OUTPUT'
Execute sp_executesql
@sql,
@param,
@totalOUT = @total OUTPUT
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93801 Visits: 20670
bswhipp - Tuesday, December 26, 2017 7:12 AM
bswhipp - Tuesday, December 26, 2017 6:54 AM
Eirikur Eiriksson - Tuesday, December 26, 2017 6:33 AM
bswhipp - Tuesday, December 26, 2017 6:07 AM
To all:. Happy Holidays. I need some help on this statement.

Currently this works but I need to change it to allow for the ability to change a database name.
@nextmonth is determined above and is set to the first day of the next month.

Declare @OpenCNFtotal numeric(15,5)
Set @OpenCNFtotal = (Select Coalesce(sum(BackOAmt), 0) as OCO
From M2MAux01..GetBackOrderDetails_View
where 1=1 and (fduedate < @nextmonth) and (Upper(fStatus) = 'OPEN') and (ShipdtCnfm = 1))

What I want to be able to do is change M2MAux01 to M2MAux + a number based on what database they are in.

I have tried but for some reason can't get the syntax correct. Any help would be much appreciated. Thanks

Declare @sql nvarchar(2500)
Declare @co char(2) = '01'

SET @sql = 'Select Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @nextmonth)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'

Exec sp_executesql
@query = @sql,
@params = N'@OpenCNFTotal Numeric output',
@OpenCNFtotalOUT = @OpenCNFtotal OUTPUT

First of all you are missing the specs of the numeric (highlighted above), secondly, simply add a USE [DATABASE_NAME] in front of the statement to execute it in another database.
Cool


Thanks for the quick reply. I added the specs but still get an error "The parameterized query '(@OpenCNFTotal Numeric(15,5) output)Select Coalesce(sum(BackOAmt' expects the parameter '@OpenCNFTotal', which was not supplied."

This in a report so I really don't want to change databases then change back if I don't have to. The report is run in the database or XXX01 or XXX02 and so on. Each XXX database has an alternate Auxiliary database such as XXXAUX01 goes with XXX01. and so on. The report is run from one central application and needs to be able to select Which XXXAUX database it needs depending on which database it is run from.
Thanks


GOT IT!!

Declare @total Numeric(15,5)
Declare @sql nvarchar(2500)
Declare @param nvarchar(500)
Declare @co char(2) = '01'
declare @date datetime = Cast('01/01/2018' as datetime)
Set @sql = N'Select @totalOut = Coalesce(sum(BackOAmt), 0)
FRom M2MAux'+@co+'..GetBackOrderDetails_View
where 1=1 and (fduedate < Cast('''+Convert(varchar(50), @date)+''' as datetime))
and (Upper(fStatus) = ''OPEN'') and (ShipdtCnfm = 1)'
Set @param = N'@totalOUT numeric(15,2) OUTPUT'
Execute sp_executesql
@sql,
@param,
@totalOUT = @total OUTPUT

Good stuff!
Cool

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search