Create Proc getinfo
(
#Studentid int,
#karan varchar(200) out
)
As
Begin
select #karan= Firstname+' '+Lastname from tbl_students where Studentid=#studentid
End
enter image description here
There is nothing wrong with the getinfo proc. Your exec statement is not quite right and need to declare and assign a variable to accept the returned value, and you need a select to see it
declare #name varchar(100)
set #name = ' '
execute getinfo #studentid = 11000,#karan = #name output
select #name name
note this is a sql server answer since that appears to be what you are using.
Related
I am trying to call a UDF from another UDF and hoping to get desired output. However, SQL Server is not returning anything in the second UDF.
My calls are:
DECLARE #V_F VARCHAR(200)
EXEC #V_F = dbo.fnCalcDiscount 220,'1234567812345679','Thailand'
PRINT #V_F
GO
DECLARE #V_F VARCHAR(200)
EXEC #V_F = dbo.groupsanddiscounts '1234567812345679','Thailand',220,220
PRINT #V_F
GO
Output:
110
0
Body of groupsanddiscounts:
CREATE FUNCTION [dbo].[groupsanddiscounts]
(#p_PROMOCODE VARCHAR(50),
#p_COUNTRY VARCHAR(100),
#p_TOTVAL INT,
#p_NEWSIMCOST INT)
RETURNS VARCHAR(300)
AS BEGIN
DECLARE #p_FINALVALUE VARCHAR(300)
SELECT #p_FINALVALUE = dbo.fnCalcDiscount (#p_NEWSIMCOST, #p_PROMOCODE, #p_COUNTRY)
RETURN #p_FINALVALUE
END
Body of fnCalcDiscount
CREATE FUNCTION [dbo].[fnCalcDiscount]
(#p_TOTVAL INT,
#p_PROMOCODE VARCHAR(200),
#p_COUNTRY VARCHAR(200))
RETURNS VARCHAR(300)
AS BEGIN
DECLARE #retResult VARCHAR(300)
SELECT #retResult = '110'
RETURN #retResult
END
Any idea what is going berserk?
Thanks
Try using a select:
select dbo.fnCalcDiscount(220, '1234567812345679', 'Thailand')
I'm surprised that exec doesn't simply return an error. It is usually used for stored procedures and SQL code.
While Working With SSRS, Today, i got 2 Problems 1 is Still remain Unsolved and Im going to Post Another Freaking Problem :) Well, Problem is : I've an Stored Procedure, Which Create an #Temp and Finally Use that data with PIVOT Expression. And, Stored Procedure itself runs Fine inside SSMS and From Visual Basic 6.0 too, but While Using that Procedure from SSRS report it shows an error at the Pivot Expression. Following are the Screen Shots, Please Review and Suggest me an Idea.
Here is an Stored Procedure :
ALTER PROCEDURE [dbo].[S_NRB_9_8_REPORT](#SCRCODE AS VARCHAR(20),
#CUREDATE VARCHAR(10),
#DTNAME VARCHAR(50),
#BR_CODE VARCHAR(50),
#CENTRALIZED VARCHAR(3))
WITH RECOMPILE
AS BEGIN
SET NOCOUNT ON;
DECLARE #BRCODE VARCHAR(3)
DECLARE #DTBASE VARCHAR(50)
DECLARE #CAT_TYPE_CODE VARCHAR(50)
DECLARE #AC_TYPE_SUB_TYPE_NAME VARCHAR(200)
DECLARE #CODESTR VARCHAR (1000)
DECLARE #CODESTR1 VARCHAR (1000)
SET #BRCODE=''
SET #DTBASE=''
SET #AC_TYPE_SUB_TYPE_NAME=''
SET #CODESTR=''
SET #CODESTR1=''
SELECT TOP 1 #CAT_TYPE_CODE=CAT_TYPE_CODE FROM REPORT_CAT_TYPE_CODE WHERE SCREEN_CODE =#SCRCODE
IF #CAT_TYPE_CODE='' OR #CAT_TYPE_CODE IS NULL
RETURN
CREATE TABLE [dbo].[#TEMPACTYPE](
[BR_CODE] [varchar](3) NULL,
[CN] [varchar](50) NULL,
[CS] [varchar](50) NULL,
[BAL] decimal(18, 2) NULL,
[AC_TYPE_SUB_TYPE_NAME] [varchar](50) NULL
) ON [PRIMARY]
IF LEN(#BR_CODE)>0
EXEC('DECLARE CUR INSENSITIVE CURSOR FOR SELECT BR_CODE FROM '+#DTBASE+'.DBO.BRANCH B (NOLOCK) WHERE BR_CODE='''+#BR_CODE+''' AND INTEGRATED=''YES'' AND APPROVED=''YES'' ORDER BY BR_CODE')
ELSE
EXEC('DECLARE CUR INSENSITIVE CURSOR FOR SELECT BR_CODE FROM '+#DTBASE+'.DBO.BRANCH B (NOLOCK) WHERE INTEGRATED=''YES'' AND APPROVED=''YES'' ORDER BY BR_CODE')
OPEN CUR
FETCH NEXT FROM CUR INTO #BRCODE
While ##FETCH_STATUS = 0
Begin
IF #CENTRALIZED='YES'
SET #DTBASE = #DTNAME
ELSE
SET #DTBASE = Left(#DTNAME, 13) + #BRCODE
EXEC('INSERT INTO #TEMPACTYPE
SELECT '''+#BRCODE+''' AS BR_CODE,T1.CAT_NAME AS CN,T1.CODES AS CS,SUM(T1.C_BAL)AS BAL,T1.AC_TYPE_SUB_TYPE_NAME FROM
(SELECT C_BAL,ATST.AC_TYPE_SUB_TYPE_NAME,CD.CAT_NAME,CD.CODE_STRING AS CODES
FROM
(SELECT AC_GROUP_CODE,CUR_CODE,GL_CODE FROM '+#DTBASE+'.dbo.AC_GROUP_GL_MAP WHERE NAMED_AC_CODE =''0301'') MAP,
(SELECT AC_GROUP_CODE,CUR_CODE,AC_NO FROM '+#DTBASE+'.dbo.DEPOSIT_AC_MAST WHERE BR_CODE='''+#BRCODE+''') DAM,
(SELECT TRAN_DATE,AC_NO,GL_CODE,PRODUCT_CODE,SUM(CLS_BAL) AS C_BAL FROM '+#DTBASE+'.dbo.AC_BAL WHERE BR_CODE='''+#BRCODE+''' GROUP BY TRAN_DATE,AC_NO,GL_CODE,PRODUCT_CODE) WD,
(SELECT * FROM '+#DTBASE+'.dbo.CAT_CODING where BR_CODE='''+#BRCODE+''' AND CAT_TYPE_CODE ='''+#CAT_TYPE_CODE+''') AS CC,
(SELECT * FROM '+#DTBASE+'.dbo.AC_TYPE_SUB_TYPE) AS ATST,
(SELECT * FROM '+#DTBASE+'.dbo.AC_GROUP) AS AG,
(SELECT * FROM '+#DTBASE+'.dbo.CAT_DETL) AS CD
WHERE
DAM.AC_GROUP_CODE =MAP.AC_GROUP_CODE
AND DAM.CUR_CODE =MAP.CUR_CODE
AND WD.GL_CODE =MAP.GL_CODE
AND CC.ENTITY_NO=DAM.AC_NO
AND ATST.AC_TYPE_CODE=AG.AC_TYPE_CODE
AND ATST.AC_TYPE_SUB_TYPE_CODE=AG.AC_TYPE_SUB_TYPE_CODE
AND AG.AC_GROUP_CODE=DAM.AC_GROUP_CODE
AND CD.CAT_TYPE_CODE=CC.CAT_TYPE_CODE
AND CD.CAT_CODE=CC.CAT_CODE
AND CD.CAT_TYPE_CODE='''+#CAT_TYPE_CODE+'''
AND WD.TRAN_DATE = (SELECT MAX(TRAN_DATE) FROM '+#DTBASE+'.dbo.AC_BAL WHERE BR_CODE ='''+#BRCODE+''' AND AC_NO = DAM.AC_NO AND TRAN_DATE <='''+#CUREDATE+''' AND GL_CODE=MAP.GL_CODE)
AND DAM.AC_NO=WD.AC_NO
UNION ALL
SELECT 0,ATST.AC_TYPE_SUB_TYPE_NAME,CAT_NAME,CODE_STRING AS CODES FROM '+#DTBASE+'.dbo.CAT_DETL AS CD,'+#DTBASE+'.dbo.AC_TYPE_SUB_TYPE AS ATST
WHERE CAT_TYPE_CODE='''+#CAT_TYPE_CODE+''' AND CAT_CODE NOT IN (SELECT CAT_CODE FROM '+#DTBASE+'.dbo.CAT_CODING WHERE BR_CODE='''+#BRCODE+''' AND CAT_TYPE_CODE='''+#CAT_TYPE_CODE+''')
AND ATST.AC_TYPE_CODE=''03''
) T1
GROUP BY T1.AC_TYPE_SUB_TYPE_NAME,CAT_NAME,CODES
ORDER BY CODES
')
FETCH NEXT FROM CUR INTO #BRCODE
END
DEALLOCATE CUR
DECLARE CUR INSENSITIVE CURSOR FOR SELECT DISTINCT AC_TYPE_SUB_TYPE_NAME FROM #TEMPACTYPE
OPEN CUR
Fetch Next from CUR Into #AC_TYPE_SUB_TYPE_NAME
While ##FETCH_STATUS = 0
Begin
IF #CODESTR =''
BEGIN
SET #CODESTR = 'ISNULL(['+#AC_TYPE_SUB_TYPE_NAME+'],0) AS ['+#AC_TYPE_SUB_TYPE_NAME+']'
SET #CODESTR1 = '['+#AC_TYPE_SUB_TYPE_NAME+']'
END
ELSE
BEGIN
SET #CODESTR = #CODESTR+',ISNULL(['+#AC_TYPE_SUB_TYPE_NAME+'],0) AS ['+#AC_TYPE_SUB_TYPE_NAME+']'
SET #CODESTR1 = #CODESTR1+',['+#AC_TYPE_SUB_TYPE_NAME+']'
END
Fetch Next from CUR Into #AC_TYPE_SUB_TYPE_NAME
END
DEALLOCATE CUR
EXEC ('Select CS,CN,'+#CODESTR+',TOTAL
from (Select CN,CS,BAL,[AC_TYPE_SUB_TYPE_NAME] from #TEMPACTYPE) ps pivot (SUM([BAL])
for [AC_TYPE_SUB_TYPE_NAME] in ('+#CODESTR1+',TOTAL)) pvt
Order by CS')
DROP TABLE #TEMPACTYPE
END
GO
And, the Dataset Design Panel :
But, Stored Procedure Runs Well inside SSMS :
Im Using SSRS 2008 R2.
Please Help me out.
And, Thanks In Advance.
Try validating that you are passing the same parameter values between the report and SSMS. You can do this by clicking Edit Query and inputting the actual parameter values. If the Edit Query window returns proper results, then you are probably passing different values to the stored procedure.
I have a view that returns users projects and also their windows login. An example of the data is below:
project | Login
------------------
project 1 | richab
project 2 | stevej
I need to append the domain to the login. I could put this in the code but i dont want to do that in every view I ever create that pulls users logins.
Can I create a global variable that I can reference in the views code. How can I achieve this? What's best practice for this?
I don't know if the SQL Server has global variables, but you can use a user defined function as follows:
CREATE FUNCTION dbo.fn_GetDomainName()
RETURNS STRING
AS
BEGIN
RETURN 'domain_name\\'
END
and do a SELECT dbo.fn_GetDomainName() + Login FROM table WHERE ... at the corresponding locations in your views.
There's no such thing as a global variable in SQL Server.
You can't just do:
DECLARE ##GlobalVar int
You can fake it with CONTEXT_INFO but to use something that would last beyond a session or restart you need to do something like this:
USE master
IF OBJECT_ID('dbo.sp_GlobalVariables') IS NOT NULL
DROP TABLE dbo.sp_GlobalVariables
GO
CREATE TABLE dbo.sp_GlobalVariables
(
varName NVARCHAR(100) COLLATE Latin1_General_CS_AI,
varValue SQL_VARIANT
)
GO
IF OBJECT_ID('dbo.sp_GetGlobalVariableValue') IS NOT NULL
DROP PROC dbo.sp_GetGlobalVariableValue
GO
CREATE PROC dbo.sp_GetGlobalVariableValue
(
#varName NVARCHAR(100),
#varValue SQL_VARIANT = NULL OUTPUT
)
AS
SET NOCOUNT ON
-- set the output parameter
SELECT #varValue = varValue
FROM sp_globalVariables
WHERE varName = #varName
-- also return it as a resultset
SELECT varName, varValue
FROM sp_globalVariables
WHERE varName = #varName
SET NOCOUNT OFF
GO
IF OBJECT_ID('dbo.sp_SetGlobalVariableValue') IS NOT NULL
DROP PROC dbo.sp_SetGlobalVariableValue
GO
CREATE PROC dbo.sp_SetGlobalVariableValue
(
#varName NVARCHAR(100),
#varValue SQL_VARIANT,
#result CHAR(1) = NULL OUTPUT
)
AS
SET NOCOUNT ON
UPDATE dbo.sp_GlobalVariables
SET varValue = #varValue
WHERE varName = #varName;
-- if it doesn't exist yet add it
IF ##rowcount = 0
BEGIN
INSERT INTO dbo.sp_GlobalVariables(varName, varValue)
SELECT #varName, #varValue
-- return it as inserted
SELECT #result = 'I'
END
-- return it as updated
SELECT #result = 'U'
SET NOCOUNT OFF
GO
DECLARE #dt DATETIME
SELECT #dt = GETDATE()
EXEC sp_SetGlobalVariableValue 'GlobalDate', #dt;
EXEC sp_SetGlobalVariableValue 'GlobalInt', 5;
EXEC sp_SetGlobalVariableValue 'GlobalVarchar', 'This is a very good global variable'
EXEC sp_SetGlobalVariableValue 'GlobalBinary', 0x0012314;
GO
EXEC sp_GetGlobalVariableValue 'GlobalDate'
EXEC sp_GetGlobalVariableValue 'GlobalInt'
EXEC sp_GetGlobalVariableValue 'GlobalVarchar'
EXEC sp_GetGlobalVariableValue 'GlobalBinary'
GO
-- update value in master
EXEC sp_SetGlobalVariableValue 'GlobalVarchar', 'New varchar value'
USE AdventureWorks
EXEC sp_GetGlobalVariableValue 'GlobalDate'
EXEC sp_GetGlobalVariableValue 'GlobalInt'
EXEC sp_GetGlobalVariableValue 'GlobalVarchar'
EXEC sp_GetGlobalVariableValue 'GlobalBinary'
-- update value in AdventureWorks
EXEC sp_SetGlobalVariableValue 'GlobalInt', 6
EXEC sp_GetGlobalVariableValue 'GlobalDate'
EXEC sp_GetGlobalVariableValue 'GlobalInt'
EXEC sp_GetGlobalVariableValue 'GlobalVarchar'
EXEC sp_GetGlobalVariableValue 'GlobalBinary'
You can use a temp table.
My scenario is that when data is updated via a known process, it adds a note to the audit table stating that 'this was done on purpose'.
When that proc fires, it inserts a single value into #auditnote (which is a temp table I create on the fly).
The trigger checks for that table. If it exists, it pulls off the note and puts it on the audit table.
If it doesn't, then it goes about it's business.
I looked at using an ## variable, but the trick there is determining if the variable exists. I don't see a way.
EXAMPLE:
Stored Procedure:
SELECT 'Alrighty Then' AS NOTE INTO #AuditNote
Trigger:
DECLARE #noteExists BIT
DECLARE #note NVARCHAR(500)
IF OBJECT_ID('tempdb..#auditnote') IS NOT NULL
BEGIN
SELECT
TOP (1)
#noteExists = 1,
#note = Note
FROM
#auditNote
END ELSE BEGIN
SELECT #noteExists = 0
END
-- do something with the note
IF #noteExists = 1
BEGIN
DROP TABLE #AuditNotes
END
I'm using #noteExists rather than a null check because someone could insert a null as the note, so we don't know if null means TABLE DOESN'T EXIST or NOTE IS NULL.
I have A stored procedure written
DECLARE #AreaID AS INT
DECLARE #DayPrior AS INT
DECLARE #TableName AS VARCHAR(50)
DECLARE #StoreQuery AS NVARCHAR(MAX)
DECLARE #SQL_ExtractDlSql AS NVARCHAR(MAX)
DECLARE #ParameterDefinition AS NVARCHAR(2000)
SET #AreaID = 1
SET #DayPrior = 1
SET #TableName = 'Tbl_Lube'
SET #SQL_ExtractDlSql = 'SELECT Download_SQL From
HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting
Where AreaId=#AreaID AND TableName=#TableName'
SET #ParameterDefinition = '#AreaID INT,#DayPrior INT,#TableName VARCHAR(50)'
EXECUTE sp_executesql #SQL_ExtractDlSql,
#ParameterDefinition,
#AreaID,
#DayPrior,
#TableName
PRINT #SQL_ExtractDlSql
I above Stored procedure, I get What I wanted on print, but instead of doing that, I wanted the selected data stores as a variable.
Then what I done is I declared a variable #StoreSql and made some modified into the SQL statement show below:
Variable Declaration:
DECLARE #StoreSql AS NVARCHAR(MAX)
Modified the Code:
SET #SQL_ExtractDlSql = 'SELECT #StoreSql = Download_SQL From
HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting
Where AreaId=#AreaID AND TableName=#TableName'
I executed the code, I get error Must declare the scalar variable "#StoreSql".
I have totally no idea how to deal with this.Any one can help.Beside,I am new to sqlserver,new to stored procedures.
Please don't hesitate to point out my any mistake,any..such as logic or etc. I learn from mistake.
Regards
LiangCk
The dynamic SQL string cannot reference variables from the outer scope. You need to declare it as an output parameter
SET #ParameterDefinition = '#AreaID INT,
#DayPrior INT,
#TableName VARCHAR(50),
#StoreSql NVARCHAR(MAX) OUTPUT'
EXECUTE sp_executesql #SQL_ExtractDlSql,
#ParameterDefinition,
#AreaID,
#DayPrior,
#TableName,
#StoreSql OUTPUT
this is my first stored procedure . i find it very difficult to debug it . help me by spending a little time on this
create procedure myworld.perform_target_proc(
IN inp_usr_id integer,
IN inp_tgt_src_id integer,
IN inp_tgt_src_type varchar(30),
IN inp_tgt_usr_id integer,
IN tgt_usr_msg text,
out tgt_res varchar(30)
)
BEGIN
declare target_count integer
select count(target_id) from target where usr_id=inp_usr_id and tgt_src_id=inp_tgt_src_id and tgt_src_type=inp_tgt_src_type
and tgt_usr_id=inp_tgt_usr_id into target_count
if target_count=0 then
begin
insert into target(usr_id, tgt_src_id, tgt_src_type, tgt_usr_id, tgt_usr_msg) values
(inp_usr_id, inp_tgt_src_id, inp_tgt_src_type, inp_tgt_usr_id, inp_tgt_usr_msg)
set tgt_res = 'new target created'
end
else
set tgt_res = 'target already exist'
end if
END |
Looks like you are missing some semicolons.
create procedure myworld.perform_target_proc(
IN inp_usr_id integer,
IN inp_tgt_src_id integer,
IN inp_tgt_src_type varchar(30),
IN inp_tgt_usr_id integer,
IN tgt_usr_msg text,
out tgt_res varchar(30)
)
BEGIN
declare target_count integer;
select count(target_id) from target where usr_id=inp_usr_id and tgt_src_id=inp_tgt_src_id and tgt_src_type=inp_tgt_src_type
and tgt_usr_id=inp_tgt_usr_id into target_count;
if target_count=0 then
insert into target(usr_id, tgt_src_id, tgt_src_type, tgt_usr_id, tgt_usr_msg) values
(inp_usr_id, inp_tgt_src_id, inp_tgt_src_type, inp_tgt_usr_id, inp_tgt_usr_msg)
set tgt_res = 'new target created';
else
set tgt_res = 'target already exist';
end if;
END |
I'm a SQL Server guy, not a MySQL guy, but in SQL Server this wouldn't even compile: variables need # prefix for one thing. My guess is you want
set #target_count = SELECT count(target_id)....
or better yet avoid the local variable altogether.