com.microsoft.sqlserver.jdbc.SQLServerException: Ambiguous column name 'Incident_id' - sql-server

Below store procedure gives following error
com.microsoft.sqlserver.jdbc.SQLServerException: Ambiguous column name 'Incident_id'
USE [FusionSysLive]
GO
/****** Object: StoredProcedure [dbo].[RCPT_Search_Reports] Script ****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[RCPT_Search_Reports]
#pagesize int = 10,
#pagestart int= 0,
#querysrting varchar(20) = '',
#orderBy varchar(40)= '',
#incident_id numeric(9) =null,
#searchdate varchar(20) =null ,
#search_string varchar(40) =null ,
#action_type varchar(40) =null ,
#security_grp_id numeric(10),
#company_id numeric(10)
as
set nocount on
set quoted_identifier off
CREATE TABLE #Temp (
Row numeric(10, 0) ,
Incident_id numeric(10, 0) null,
Description varchar(50),
Creation_timestamp varchar(100) null,
)
set nocount on
set quoted_identifier off
declare #query varchar(max)
set #query = 'select ROW_NUMBER() OVER (order by '+#orderBy +') AS Row,inc.Incident_id, inc.Description, inc.Creation_timestamp from Incident inc, Incident_list incl where inc.Incident_id = incl.Incident_id '
If(#security_grp_id = 1)
Begin
Set #query = #query + 'and (inc.Company_id ='+cast(#company_id as varchar)+' or inc.Company_id is null) '
End
Else
Begin
Set #query = #query + 'and inc.Company_id ='+cast(#company_id as varchar)
End
If ( ( #incident_id<>0 and #incident_id is not null) or ( (#action_type = 'byGroupID' or #action_type = 'byGroupName' ) and #incident_id=0 and #search_string='' and #searchdate='' ))
Begin
Set #query = #query + ' and inc.Incident_id=' + cast(#incident_id as varchar)
End
If (#action_type is not null and #search_string is not null and #action_type!='' and #search_string!='' )
Begin
If (#action_type = 'byGroupID')
Begin
Set #query = #query + ' and incl.Recipient_list_id in (select Group_id from Contact_group where Group_id like '''+ #search_string + '%'') '
End
Else If (#action_type = 'byGroupName')
Begin
Set #query = #query + ' and incl.Recipient_list_id in (select Group_id from Contact_group where Group_name like '''+ #search_string + '%'') '
End
End
If (#searchdate is not null and #searchdate!='' )
Begin
Set #query = #query + ' and CONVERT(datetime, FLOOR(CONVERT(float(24), Creation_timestamp))) =''' + #searchdate + ''''
End
set #query = #query + ' order by inc.Incident_id';
print #query
insert into #Temp exec(#query)
select row ,Incident_id , Description , Creation_timestamp
from #Temp where Row > #pagestart and row <= (#pagestart + #pagesize)
select count(*) as total_count from #Temp
drop table #Temp

Related

An OLE DB error has occurred. Error code: 0x80040E14, Invalid column name

I have an ETL package built in SSIS that I'm trying to run but I'm getting this error, mainly the third one:
The part in the package that is giving the error is built like this:
The specific component that is causing the error is the call to the SP and this are the parameters:
The parameters are translated to:
The parameters come from the query done at the start of the data flow:
The error mentions invalid column 'P2' the only column that takes this value is SG_PLANO_CTB.
This is the SP that's being used:
USE [SISF_DW_REPORTING]
GO
/****** Object: StoredProcedure [dbo].[SP_INSERT_EAF_MEMBER] Script Date: 8/10/2018 11:22:07 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: SQLQuery64.sql|7|0|C:\Users\SQL_AD~1.CMC\AppData\Local\Temp\3\~vs4CBB.sql
-- Batch submitted through debugger: SQLQuery29.sql|7|0|C:\Users\SQL_AD~1.CMC\AppData\Local\Temp\3\~vs8A81.sql
-- =============================================
-- Author:
-- Create date:
-- Description: This stored procedure creates the Early Arriving Fact for a given reference table
-- =============================================
ALTER PROCEDURE [dbo].[SP_INSERT_EAF_MEMBER]
#TABLE NVARCHAR(50)
,#CD_REF NVARCHAR(50)
,#LOG_ID INT
,#ID_SK_COMPOSITE NVARCHAR(50) = NULL
,#COL_SK_COMPOSITE NVARCHAR(50) = NULL
,#DT_LOAD_DATE INT = NULL
,#TABLE_FCT NVARCHAR(50) = NULL
,#DEBUG BIT = 0
AS
BEGIN
SET NOCOUNT ON
IF 1=2
BEGIN
SELECT CAST(NULL AS INT) AS ID_REF
,CAST(NULL AS NVARCHAR(50)) AS DS_TBL_NAME
,CAST(NULL AS NVARCHAR(50)) AS CD_SRCE_SYTM
,CAST(NULL AS INT) AS LOG_ID
,CAST(NULL AS INT) AS DT_LOAD_DATE
,CAST(NULL AS NVARCHAR(50)) AS DS_TBL_FCT_NAME
END
--****************************************************
SET #TABLE = LTRIM(RTRIM(#TABLE))
SET #CD_REF = LTRIM(RTRIM(#CD_REF))
SET #TABLE_FCT = LTRIM(RTRIM(#TABLE_FCT))
--****************************************************
DECLARE #ID_INT_EAF INT
DECLARE #ID_EAF NVARCHAR(100)
DECLARE #DS_EAF NVARCHAR(100)
DECLARE #DT_INT_EAF NVARCHAR(100)
DECLARE #DT_DAT_EAF NVARCHAR(100)
DECLARE #CD_INT_EAF NVARCHAR(100)
DECLARE #CD_VAR_EAF NVARCHAR(100)
DECLARE #FL_EAF NVARCHAR(4)
DECLARE #NR_INT_EAF NVARCHAR(100)
DECLARE #NR_VAR_EAF NVARCHAR(100)
DECLARE #QT_EAF NVARCHAR(100)
DECLARE #VL_EAF NVARCHAR(100)
DECLARE #DMS_EAF NVARCHAR(100)
DECLARE #GPS_EAF NVARCHAR(100)
DECLARE #WGS_EAF NVARCHAR(100)
DECLARE #DT_CRTN NVARCHAR(100)
DECLARE #QUERY_FCT_LOAD_EAF VARCHAR(7000)
DECLARE #QUERY_REF_EAF VARCHAR(7000)
DECLARE #SG_EAF NVARCHAR(100)
DECLARE #HR_EAF NVARCHAR(100)
SET #ID_EAF = '-1'
SET #DS_EAF = '''EAF Member ('+#CD_REF+')'''
SET #DT_INT_EAF = '-1'
SET #DT_DAT_EAF = '''1900-01-01'''
SET #CD_VAR_EAF = ''''+#CD_REF+''''
SET #CD_INT_EAF = '-1'
SET #FL_EAF = '''-1'''
SET #NR_INT_EAF = '0'
SET #NR_VAR_EAF = '''EAF Member'''
SET #QT_EAF = '''0'''
SET #VL_EAF = '''0'''
SET #DMS_EAF = '''EAF Member'''
SET #GPS_EAF = '''EAF Member'''
SET #WGS_EAF = '-1'
SET #DT_CRTN = CONVERT(NVARCHAR(8),GETDATE(),112)
SET #SG_EAF = '''EAF'''
SET #HR_EAF = '''00:00:00'''
-- Declare auxiliary variables
DECLARE #TABLE_NAME NVARCHAR(50), #COLUMN_NAME NVARCHAR(100), #EAF_VALUE NVARCHAR(100)
DECLARE #INSERT NVARCHAR(3000), #VALUES NVARCHAR(3000), #WHERE NVARCHAR(1000), #IDENTITY_ON NVARCHAR(1000), #IDENTITY_OFF NVARCHAR(1000)
DECLARE #STATEMENT VARCHAR(7000)
SET #IDENTITY_ON = 'SET IDENTITY_INSERT ' + #TABLE + ' ON;'
SET #IDENTITY_OFF = 'SET IDENTITY_INSERT ' + #TABLE + ' OFF;'
SET #INSERT = 'INSERT INTO ' + #TABLE + ' ('
SET #VALUES = ' SELECT '
BEGIN
IF #COL_SK_COMPOSITE IS NULL
SET #WHERE = ' WHERE NOT EXISTS (SELECT 1 FROM ' + #TABLE + ' WHERE CD_' + SUBSTRING(#TABLE,9,LEN(#TABLE)) + ' = '''+#CD_REF+''');'
ELSE
SET #WHERE = ' WHERE NOT EXISTS (SELECT 1 FROM ' + #TABLE + ' WHERE CD_' + SUBSTRING(#TABLE,9,LEN(#TABLE)) + ' = '''+#CD_REF+''' AND ' + #COL_SK_COMPOSITE + ' = ' + #ID_SK_COMPOSITE + ');'
END
DECLARE TABLE_COLUMNS CURSOR FOR
SELECT
C.TABLE_NAME
,C.COLUMN_NAME
,CASE
WHEN #COL_SK_COMPOSITE IS NOT NULL AND LEFT(C.NEW_COLUMN_NAME,2) LIKE 'ID' AND C.NEW_COLUMN_NAME NOT LIKE 'ID_'+SUBSTRING(C.TABLE_NAME,5,LEN(C.TABLE_NAME)) AND C.NEW_COLUMN_NAME NOT LIKE 'ID_'+SUBSTRING(C.TABLE_NAME,9,LEN(C.TABLE_NAME))
THEN #ID_SK_COMPOSITE
WHEN #COL_SK_COMPOSITE IS NULL AND LEFT(C.NEW_COLUMN_NAME,2) LIKE 'ID' AND C.NEW_COLUMN_NAME NOT LIKE 'ID_'+SUBSTRING(C.TABLE_NAME,5,LEN(C.TABLE_NAME)) AND C.NEW_COLUMN_NAME NOT LIKE 'ID_'+SUBSTRING(C.TABLE_NAME,9,LEN(C.TABLE_NAME))
THEN #ID_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'DS'
THEN #DS_EAF
WHEN C.NEW_COLUMN_NAME = 'DT_START' THEN '''1900-01-01'''
WHEN C.NEW_COLUMN_NAME = 'DT_END' THEN '''9999-12-31'''
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'DT' THEN
CASE
WHEN C.NEW_COLUMN_NAME LIKE 'DT_CRTN' THEN #DT_CRTN
WHEN C.DATA_TYPE LIKE 'int' THEN #DT_INT_EAF
ELSE #DT_DAT_EAF END
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'CD' THEN
CASE WHEN C.DATA_TYPE LIKE 'int' THEN #CD_INT_EAF ELSE #CD_VAR_EAF END
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'FL'
THEN #FL_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'NR' THEN
CASE
WHEN C.DATA_TYPE LIKE 'int' THEN #NR_INT_EAF
WHEN C.DATA_TYPE LIKE 'numeric' THEN #NR_INT_EAF
ELSE #NR_VAR_EAF END
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'QT'
THEN #QT_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'VL'
THEN #VL_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,3) LIKE 'DMS'
THEN #DMS_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,3) LIKE 'GPS'
THEN #GPS_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,3) LIKE 'WGS'
THEN #WGS_EAF
WHEN C.NEW_COLUMN_NAME = 'CTL_LOG_EAF'
THEN '1'
WHEN LEFT(C.NEW_COLUMN_NAME,7) LIKE 'CTL_LOG'
THEN CAST(#LOG_ID AS NVARCHAR(50))
WHEN #COL_SK_COMPOSITE IS NOT NULL AND LEFT(C.NEW_COLUMN_NAME,2) LIKE 'SG'
THEN #ID_SK_COMPOSITE
WHEN #COL_SK_COMPOSITE IS NULL AND LEFT(C.NEW_COLUMN_NAME,2) LIKE 'SG'
THEN #SG_EAF
WHEN LEFT(C.NEW_COLUMN_NAME,2) LIKE 'HR'
THEN #HR_EAF
ELSE ''
END EAF_VALUE
FROM
(
SELECT
TABLE_NAME
,COLUMN_NAME
,CASE WHEN LEFT(COLUMN_NAME,2) LIKE 'X_' THEN SUBSTRING(COLUMN_NAME,3,LEN(COLUMN_NAME)) ELSE COLUMN_NAME END AS NEW_COLUMN_NAME
,DATA_TYPE
,ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME NOT LIKE 'ID_'+SUBSTRING(TABLE_NAME,9,LEN(TABLE_NAME))
) C
INNER JOIN
INFORMATION_SCHEMA.TABLES T
ON C.TABLE_NAME = T.TABLE_NAME
WHERE
T.TABLE_TYPE LIKE 'BASE TABLE'
AND T.TABLE_NAME LIKE #TABLE
ORDER BY
TABLE_NAME, ORDINAL_POSITION
OPEN TABLE_COLUMNS
FETCH NEXT FROM TABLE_COLUMNS INTO #TABLE_NAME, #COLUMN_NAME, #EAF_VALUE;
WHILE ##FETCH_STATUS = 0
BEGIN
IF #COLUMN_NAME <> ''
BEGIN
SET #INSERT = #INSERT + #COLUMN_NAME + ', '
END
IF #EAF_VALUE <> ''
BEGIN
SET #VALUES = #VALUES + #EAF_VALUE + ', '
END
FETCH NEXT FROM TABLE_COLUMNS INTO #TABLE_NAME, #COLUMN_NAME, #EAF_VALUE;
END
CLOSE TABLE_COLUMNS
DEALLOCATE TABLE_COLUMNS
-- Final columns
SET #INSERT = LEFT(#INSERT,LEN(#INSERT)-1) + ')'
SET #VALUES = LEFT(#VALUES,LEN(#VALUES)-1)
SET #QUERY_FCT_LOAD_EAF = 'INSERT INTO FCT_LOAD_EAF (ID_LOAD_DATE, ID_CRTN_DATE, DS_REF_TABLE_NAME, DS_FCT_TABLE_NAME, CD_SRCE_SYTM,CTL_LOG_INSERT) SELECT '+CAST(#DT_LOAD_DATE AS NVARCHAR(8))+', '+#DT_CRTN+', '''+#TABLE+''', '''+#TABLE_FCT+''', '''+#CD_REF+''', '+CAST(#LOG_ID AS NVARCHAR(50))+' '+#WHERE
IF #DEBUG = 1 BEGIN SELECT #QUERY_FCT_LOAD_EAF END ELSE BEGIN EXECUTE(#QUERY_FCT_LOAD_EAF) END
PRINT #QUERY_FCT_LOAD_EAF
SET #STATEMENT = #INSERT + #VALUES + #WHERE
IF #DEBUG = 1 BEGIN SELECT #STATEMENT END ELSE BEGIN EXECUTE(#STATEMENT) END
PRINT #STATEMENT
IF LEFT(#TABLE,2) = 'X_'
BEGIN
SET #QUERY_REF_EAF = 'SELECT X_ID_'+SUBSTRING(#TABLE,7,LEN(#TABLE))+' AS ID_REF, ''' + #TABLE + ''' AS DS_TBL_NAME, CAST(X_CD_SRCE_SYTM AS NVARCHAR(50)) AS CD_SRCE_SYTM, '+CAST(#LOG_ID AS NVARCHAR(50))+' AS LOG_ID, '+CAST(#DT_LOAD_DATE AS NVARCHAR(8))+' AS DT_LOAD_DATE , ''' + #TABLE_FCT + ''' AS DS_TBL_FCT_NAME FROM '+#TABLE+' WHERE X_CD_SRCE_SYTM LIKE '''+#CD_REF+''''
IF #DEBUG = 1 BEGIN SELECT #QUERY_REF_EAF END ELSE BEGIN EXECUTE(#QUERY_REF_EAF) END
PRINT #QUERY_REF_EAF
END
ELSE
BEGIN
SET #QUERY_REF_EAF = 'SELECT ID_'+SUBSTRING(#TABLE,5,LEN(#TABLE))+' AS ID_REF, ''' + #TABLE + ''' AS DS_TBL_NAME, CAST(CD_SRCE_SYTM AS NVARCHAR(50)) AS CD_SRCE_SYTM, '+CAST(#LOG_ID AS NVARCHAR(50))+' AS LOG_ID, '+CAST(#DT_LOAD_DATE AS NVARCHAR(8))+' AS DT_LOAD_DATE, ''' + #TABLE_FCT + ''' AS DS_TBL_FCT_NAME FROM '+#TABLE+' WHERE CD_SRCE_SYTM LIKE '''+#CD_REF+''''
IF #DEBUG = 1 BEGIN SELECT #QUERY_REF_EAF END ELSE BEGIN EXECUTE(#QUERY_REF_EAF) END
PRINT #QUERY_REF_EAF
END
SET NOCOUNT OFF
END
I tried debugging the SP but I can't figure out where he builds the query that uses 'P2' as a column and not as value of the column SG_PLANO_CTB
Edit: I decided to log the parameters that were being used. Found out that the one causing the call causing the error is
exec SISF_DW_REPORTING..SP_INSERT_EAF_MEMBER 'REF_FIN_RUBRICA','11.1.1', 210999, 'P2', 'SG_PLANO_CTB'
And the query that's causing the error is
INSERT INTO REF_FIN_RUBRICA (CD_RUBRICA, DS_RUBRICA, CD_KEY, CD_PARENT, SG_PLANO_CTB, DT_START, DT_END, CTL_LOG_UPDATE, CTL_LOG_EAF) SELECT '11.1.1', 'EAF Member (11.1.1)', '11.1.1', '11.1.1', P2, '1900-01-01', '9999-12-31', 210999, 1 WHERE NOT EXISTS (SELECT 1 FROM REF_FIN_RUBRICA WHERE CD_RUBRICA = '11.1.1' AND SG_PLANO_CTB = P2);
I would guess the string delimitators aren't being added in the cursor somewhere. Can't see where though.
I tried to step through your generated proc logically, and I think I see where the issue lies. The call that's causing the error, exec SISF_DW_REPORTING..SP_INSERT_EAF_MEMBER 'REF_FIN_RUBRICA','11.1.1', 210999, 'P2', 'SG_PLANO_CTB' should probably be changed to:
exec SISF_DW_REPORTING..SP_INSERT_EAF_MEMBER 'REF_FIN_RUBRICA','11.1.1', 210999, '''P2''', 'SG_PLANO_CTB'
It looks like the dynamic sql generation takes the value of one of your input variables and uses that in the query string it builds. If your query needs a string value, you have to additionally double delimit it.

How to search with parameter colums in a stored procedure?

I'm trying to write a stored procedure in SQL Server that gets the columns as parameters. The user will select the column name from a combo box and will write the searched value for that column on a textbox.
I've been searching how to do this and so far i have this:
ALTER PROCEDURE [dbo].[SP_Select_TBL_Folio]
#cant int,
#Column1 nvarchar(50),
#Value1 nvarchar(50),
#Column2 nvarchar(50),
#Value2 nvarchar(50),
#Column3 nvarchar(50),
#Value3 nvarchar(50)
AS
BEGIN
declare #query nvarchar (max)
SET NOCOUNT ON;
if #cant = 1
BEGIN
set #query = 'SELECT * FROM TBL_Folio WHERE ' + #Column1 + ' LIKE '+ #Value1 + ' ORDER BY 1 DESC';
exec sp_executesql #query, N' '
END
else
BEGIN
if #cant = 2
BEGIN
set #query = 'SELECT * FROM TBL_Folio WHERE ' + #Column1 + ' LIKE '+ #Value1 + ' AND ' + #Column2 + ' LIKE '+ #Value2 + ' ORDER BY 1 DESC';
exec sp_executesql #query, N' '
END
ELSE
if #cant = 3
BEGIN
set #query = 'SELECT * FROM TBL_Folio WHERE ' + #Column1 + ' LIKE '+ #Value1 + ' AND ' + #Column2 + ' LIKE '+ #Value2 + ' AND ' + #Column3 + ' LIKE '+ #Value3 + ' ORDER BY 1 DESC';
exec sp_executesql #query, N' '
END
END
END
The user can send 1 to 3 values, for that I have the parameter #cant, this code works but I want to know if there is a better way to do this or how can I improve this stored procedure.
I think what you have is fine if you need to do it in an SP rather than client side. I would probabably initialize the query to the 'select * from TBL_Folio" and then append the LIKES after each if. I would also caution against using SELECT * so your client side doesn't blow up if a field gets added to the table.
If you have a need to check a variable number of fields rather than just up to 3, you can do a table-valued parameter and build up your query by looping through. Here is an example:
ALTER PROCEDURE [dbo].[GetFilteredInvoices]
#FilterColumns ColumnValueType READONLY
AS
BEGIN
SET NOCOUNT ON;
declare #columnName varchar(50), #columnValue varchar(MAX), #query nvarchar(MAX), #count int
set #query='SELECT InvoiceNumber, InvoiceDate, Customer from Invoices '
set #count=0
set #columnName=''
while exists(select * from #FilterColumns where ColumnName>#ColumnName)
begin
set #columnName=(select min(ColumnName) from #FilterColumns where ColumnName>#columnName)
if #count=0
set #query=#query+'WHERE '
else
set #query=#query+'AND '
set #query=#query+ (select ColumnName+' Like ''%'+ColumnValue+'%'' ' from #filterColumns where ColumnName=#columnName)
set #count=#count+1
end
exec sp_executesql #query
END
Here is the table valued type I used:
CREATE TYPE [dbo].[ColumnValueType] AS TABLE(
[ColumnName] [varchar](50) NULL,
[ColumnValue] [varchar](max) NULL
)
GO
Now this will take any number of columns and values to apply the filter.
Here is an example call to the procedure:
DECLARE #RC int
DECLARE #FilterColumns [dbo].[ColumnValueType]
insert into #filterColumns
Values('InvoiceNumber','345')
,('Customer','67')
EXECUTE #RC = [dbo].[GetFilteredInvoices]
#FilterColumns
I think you can perhaps improve the way that you handle your input parameters by getting rid of the #cant parameter. You can also improve the way that you build up the conditions, at the moment you are not handling the situations where only #Column2 and #Value2 or only #Column3 and #Value3 is set (perhaps it is not needed in your case, but it is still good practice to handle these types of scenarios)
CREATE PROCEDURE SP_Select_TBL_Folio
#Column1 NVARCHAR(50) = NULL,
#Value1 NVARCHAR(50) = NULL,
#Column2 NVARCHAR(50) = NULL,
#Value2 NVARCHAR(50) = NULL,
#Column3 NVARCHAR(50) = NULL,
#Value3 NVARCHAR(50) = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE
#P1 NVARCHAR(500),
#P2 NVARCHAR(500),
#P3 NVARCHAR(500),
#SQL NVARCHAR(MAX)
IF (#Column1 IS NULL OR #Column1 = '') AND (#Value1 IS NULL OR #Value1 = '')
BEGIN
-- This will build up dynamic SQL to always select records even if #Column1
-- and #Value1 is not set. This obvisously all depends on your requirements
-- and if you still need to select records if the parameters are not set, otherwise
-- it can be changed to ' WHERE ThePrimaryKeyColumn = 0'
SET #P1 = ' WHERE ThePrimaryKeyColumn > 0'
END
ELSE
BEGIN
SET #P1 = 'WHERE ' + #Column1 + ' LIKE ' + '''' + #Value1 + ''''
END
IF (#Column2 IS NULL OR #Column2 = '') AND (#Value2 IS NULL OR #Value2 = '')
BEGIN
SET #P2 = ''
END
ELSE
BEGIN
SET #P2 = ' AND ' + #Column2 + ' LIKE ' + '''' + #Value2 + ''''
END
IF (#Column3 IS NULL OR #Column3 = '') AND (#Value3 IS NULL OR #Value3 = '')
BEGIN
SET #P3 = ''
END
ELSE
BEGIN
SET #P3 = ' AND ' + #Column3 + ' LIKE ' + '''' + #Value3 + ''''
END
SET #SQL = 'SELECT * FROM TBL_Folio
[P1]
[P2]
[P3]'
-- Here we set all the conditions
SET #SQL = REPLACE(#SQL, '[P1]', #P1);
SET #SQL = REPLACE(#SQL, '[P2]', #P2);
SET #SQL = REPLACE(#SQL, '[P3]', #P3);
-- This will be replaced by EXEC(#SQL)
PRINT #SQL
END
So now you can for instance execute
EXEC SP_Select_TBL_Folio
which will give you
SELECT * FROM TBL_Folio
WHERE ThePrimaryKeyColumn > 0
or you can execute
EXEC SP_Select_TBL_Folio 'Column1','Value1'
which will give you
SELECT * FROM TBL_Folio
WHERE Column1 LIKE 'Value1'
or you can execute
EXEC SP_Select_TBL_Folio NULL,NULL,'Column2','Value2'
which will give you
SELECT * FROM TBL_Folio
WHERE ThePrimaryKeyColumn > 0
AND Column2 LIKE 'Value2'
I'm not going to list all the permutations, I'm sure you get my point.

Procedure expects parameter '#statement' of type 'ntext/nchar/nvarchar'

Here is my query based sp:
DECLARE #GLOBALPATIENTACCOUNT VARCHAR(25)
DECLARE #Page_Index BIGINT
DECLARE #Page_Size BIGINT
DECLARE #practice_code VARCHAR(20)
DECLARE #dateFrom VARCHAR(20)
DECLARE #dateTo VARCHAR(20)
DECLARE #startFrom VARCHAR(20)
DECLARE #startTo VARCHAR(20)
DECLARE #Total_Record DECIMAL(34, 1)
DECLARE #Total_Pages DECIMAL(34, 1)
DECLARE #From_row BIGINT
DECLARE #To_row BIGINT
SET #GLOBALPATIENTACCOUNT = '999090999510103196'
SET #Page_Index = 1
SET #Page_Size = 30
SET #practice_code = 9090999
SET #dateFrom = '09/13/2014'
SET #dateTo = '10/13/2014'
SET #startFrom = 'null'
SET #startTo = 'null'
DECLARE #sqlstr AS varchar (max)
set #sqlstr='';
SET #sqlstr =#sqlstr +N'select CREATED_DTAE, DOCUMENT_CATEGORY_ID, DESCRIPTION, DOCUMENT_NAME, SHOW_ON_WEB, VIEW_BY_PATIENT, VIEW_DATE, NO_OF_IMAGES, DOCUMENT_STATUS_ID, DOCUMENT_STATUS_DESCRIPTION,
PATIENT_DOCUMENT_ID,ISDICOM,PATIENT_ACCOUNT,DOCUMENT_INDEX, ASSIGNED_TO,
CONTENT_START_DATE, COMMENTS,CHART_ID,LAST_NAME,FIRST_NAME, CONTENT_END_DATE,CREATED_BY,[CREATED DATE], MODIFIED_BY,MODIFIED_DATE,DELETED,SOURCE_PATH,CONFIDENTIAL,
DOC_UPLOAD_NAME,DOC_UPLOAD_STATUS,IS_MISC_DOC,BUTTON,SIGNED,SIGNED_BY,SIGNED_DATE, PRACTICE_CODE
into #temptbl
FROM
(
SELECT ISNULL(CREATED_DTAE,'''')as CREATED_DTAE , PATIENT_DOCUMENTS.DOCUMENT_CATEGORY_ID, ISNULL(UPPER(DT.DESCRIPTION),'''')as DESCRIPTION,
ISNULL(PATIENT_DOCUMENTS.DOCUMENT_NAME,'''') as DOCUMENT_NAME,
ISNULL(PATIENT_DOCUMENTS.SHOW_ON_WEB,''0'') AS SHOW_ON_WEB, ISNULL(PATIENT_DOCUMENTS.VIEW_BY_PATIENT,''0'') AS VIEW_BY_PATIENT,
ISNULL(CONVERT(VARCHAR(10),PATIENT_DOCUMENTS.VIEW_DATE,101),'''') AS VIEW_DATE, ISNULL(PATIENT_DOCUMENTS.NO_OF_IMAGES,''0'') AS NO_OF_IMAGES,
ISNULL((CONVERT(VARCHAR(12), CASE PATIENT_DOCUMENTS.DOCUMENT_STATUS_ID WHEN 0 THEN NULL ELSE PATIENT_DOCUMENTS.DOCUMENT_STATUS_ID END )),'''') AS DOCUMENT_STATUS_ID,
ISNULL(UPPER(DOCUMENTS_STATUS.DOCUMENT_STATUS_DESCRIPTION),'''') DOCUMENT_STATUS_DESCRIPTION , PATIENT_DOCUMENTS.PATIENT_DOCUMENT_ID,
ISNULL(PATIENT_DOCUMENTS.ISDICOM,''0'') ISDICOM, PATIENT_DOCUMENTS.PATIENT_ACCOUNT, ISNULL(PATIENT_DOCUMENTS.DOCUMENT_INDEX,0) DOCUMENT_INDEX, ISNULL(PATIENT_DOCUMENTS.ASSIGNED_TO,'''') ASSIGNED_TO ,
CONVERT(VARCHAR,PATIENT_DOCUMENTS.CONTENT_START_DATE,101) CONTENT_START_DATE, REPLACE (PATIENT_DOCUMENTS.COMMENTS,CHAR(10),'' '') AS COMMENTS, PATIENT.CHART_ID, UPPER(PATIENT.LAST_NAME)LAST_NAME,UPPER(PATIENT.FIRST_NAME) FIRST_NAME ,
CONVERT(VARCHAR,PATIENT_DOCUMENTS.CONTENT_END_DATE,101) CONTENT_END_DATE, ISNULL(UPPER(PATIENT_DOCUMENTS.CREATED_BY),'''') CREATED_BY,
CONVERT(VARCHAR,CREATED_DTAE,101) +'' ''+ LTRIM(SUBSTRING(CONVERT(VARCHAR,(CONVERT(DATETIME,CREATED_DTAE)),109),13,5))+'' ''+ SUBSTRING(CONVERT(VARCHAR,(CONVERT(DATETIME,CREATED_DTAE)),109),25,2) AS [CREATED DATE],
ISNULL(UPPER(PATIENT_DOCUMENTS.MODIFIED_BY),'''') MODIFIED_BY,
ISNULL(CONVERT(VARCHAR,PATIENT_DOCUMENTS.MODIFIED_DATE,101),'''') +'' ''+ LTRIM(SUBSTRING(CONVERT(VARCHAR,(CONVERT(DATETIME,PATIENT_DOCUMENTS.MODIFIED_DATE)),109),13,5))+'' ''+ SUBSTRING(CONVERT(VARCHAR,(CONVERT(DATETIME,PATIENT_DOCUMENTS.MODIFIED_DATE)),109),25 ,2) AS MODIFIED_DATE,PATIENT_DOCUMENTS.DELETED,ISNULL(PATIENT_DOCUMENTS.SOURCE_PATH,'''') SOURCE_PATH,ISNULL(PATIENT_DOCUMENTS.CONFIDENTIAL,0) CONFIDENTIAL,ISNULL(PATIENT_DOCUMENTS.DOC_UPLOAD_NAME,'''') DOC_UPLOAD_NAME,
ISNULL(PATIENT_DOCUMENTS.DOC_UPLOAD_STATUS,'''') DOC_UPLOAD_STATUS,CONVERT(BIT,''FALSE'') IS_MISC_DOC,'''' AS BUTTON,
PATIENT_DOCUMENTS.Signed SIGNED, PATIENT_DOCUMENTS.Sign_by SIGNED_BY, PATIENT_DOCUMENTS.Sign_date SIGNED_DATE,
PATIENT.PRACTICE_CODE AS PRACTICE_CODE
FROM PATIENT, PATIENT_DOCUMENTS
LEFT OUTER JOIN DOCUMENTS_STATUS ON PATIENT_DOCUMENTS.DOCUMENT_STATUS_ID = DOCUMENTS_STATUS.DOCUMENT_STATUS_ID,
DOCUMENT_CATEGORIES DT
WHERE PATIENT_DOCUMENTS.DOCUMENT_CATEGORY_ID = DT.DOCUMENT_CATEGORY_ID
AND PATIENT_DOCUMENTS.PATIENT_ACCOUNT = PATIENT.PATIENT_ACCOUNT AND PATIENT.Patient_GlobalId= ''' + #GLOBALPATIENTACCOUNT +'''
AND ISNULL(PATIENT.DELETED,0) <> 1
AND ISNULL(PATIENT_DOCUMENTS.DELETED, 0) <> 1
AND PATIENT.PRACTICE_CODE <> ''' + #practice_code + '''
AND PATIENT_DOCUMENTS.confidential <> 1 '
--print #sqlstr
IF (#dateFrom <> 'null')
BEGIN
SET #sqlstr = #sqlstr + ' and CONVERT(VARCHAR,ISNULL(PATIENT_DOCUMENTS.CREATED_DTAE,''''),101) >= CONVERT(VARCHAR,isnull( ''' + #dateFrom + ''',''''),101) and CONVERT(VARCHAR,ISNULL(PATIENT_DOCUMENTS.CREATED_DTAE,''''),101) <= CONVERT(VARCHAR,ISNULL(''' + #dateTo + ''',''''),101)'
--print #sqlstr
END
IF(#startFrom <> 'null')
BEGIN
SET #sqlstr = #sqlstr + ' CONVERT(VARCHAR,ISNULL(PATIENT_DOCUMENTS.CONTENT_START_DATE,''''),101) >= CONVERT(VARCHAR,ISNULL(''' + #startFrom + ''',''''),101) and CONVERT(VARCHAR,ISNULL(PATIENT_DOCUMENTS.CONTENT_START_DATE,''''),101) <= CONVERT(VARCHAR,isnull(''' + #startTo + ''',''''),101)'
END
SET #sqlstr = #sqlstr + 'UNION ALL
SELECT ISNULL(DOCUMENTS.CREATED_DATE,'''') AS CREATED_DTAE, CATAGORY_TYPE_ID AS DOCUMENT_CATEGORY_ID, ISNULL(DOCUMENT_CATEGORIES.DESCRIPTION,'''') DESCRIPTION,
(SELECT TOP 1 DOCUMENTS_CATEGORY_MAIN_PATH FROM DOCUMENTS_CATEGORY_MAIN WHERE DOCUMENTS_CATEGORY_MAIN_DESCRIPTION=''ATTACHMENTS'' )+''\'' +DOCUMENTS.FILE_NAME_FORPATH AS [DOCUMENT_NAME], isnull(DOCUMENTS.SHOW_ON_WEB,0) AS SHOW_ON_WEB,''0'' AS VIEW_BY_PATIENT
,ISNULL(NULL,'''') AS VIEW_DATE, ''0'' AS NO_OF_IMAGES,ISNULL(DOCUMENTS.DOCUMENT_STATUS_ID,'''') DOCUMENT_STATUS_ID,ISNULL(DOCUMENTS_STATUS.DOCUMENT_STATUS_DESCRIPTION,'''') DOCUMENT_STATUS_DESCRIPTION, DOC_ID AS PATIENT_DOCUMENT_ID,
ISNULL(NULL,''0'') AS ISDICOM, DOCUMENTS.PATIENT_ACCOUNT, ISNULL(NULL,0) AS DOCUMENT_INDEX,ISNULL(DOCUMENTS.ASSIGNED_TO,'''') ASSIGNED_TO, CONVERT(VARCHAR,DOCUMENTS.CONTENT_START_DATE,101) AS CONTENT_START_DATE,
ISNULL(DOCUMENTS.NOTES,'''') AS COMMENTS, '''' AS CHART_ID, UPPER(PM.LAST_NAME)LAST_NAME,UPPER(PM.FIRST_NAME) FIRST_NAME,
ISNULL(CONVERT(VARCHAR,DOCUMENTS.CONTENT_END_DATE,101),'''') AS CONTENT_END_DATE, ISNULL(DOCUMENTS.CREATED_BY,'''') CREATED_BY, ISNULL(DOCUMENTS.CREATED_DATE,'''') AS [CREATED DATE],
ISNULL(DOCUMENTS.MODIFIED_BY,'''') MODIFIED_BY, ISNULL(DOCUMENTS.MODIFIED_DATE,'''') MODIFIED_DATE, DOCUMENTS.DELETED, '''' AS SOURCE_PATH, ISNULL(DOCUMENTS.CONFIDENTIAL,0) CONFIDENTIAL, '''' AS DOC_UPLOAD_NAME, '''' AS DOC_UPLOAD_STATUS,
CONVERT(BIT,''TRUE'') IS_MISC_DOC,'''' AS BUTTON , DOCUMENTS.SIGNED AS SIGNED, DOCUMENTS.SIGN_BY AS SIGNED_BY, DOCUMENTS.SIGN_DATE AS SIGNED_DATE,
PM.PRACTICE_CODE AS PRACTICE_CODE
FROM PATIENT_DOCUMENTS_OTHERS DOCUMENTS
LEFT OUTER JOIN DOCUMENT_CATEGORIES ON DOCUMENTS.CATAGORY_TYPE_ID=DOCUMENT_CATEGORIES.DOCUMENT_CATEGORY_ID
INNER JOIN DOCUMENTS_CATEGORY_MAIN DCM ON DOCUMENTS.DOCUMENTS_CATEGORY_MAIN_ID =DCM.DOCUMENTS_CATEGORY_MAIN_ID
INNER JOIN PATIENT PM ON PM.PATIENT_ACCOUNT=DOCUMENTS.PATIENT_ACCOUNT
LEFT OUTER JOIN DOCUMENTS_STATUS ON DOCUMENTS_STATUS.DOCUMENT_STATUS_ID=DOCUMENTS.DOCUMENT_STATUS_ID
left outer join PATIENT_DOCUMENTS on DOCUMENTS.Doc_Id=PATIENT_DOCUMENTS.PATIENT_DOCUMENT_ID
WHERE ISNULL(DOCUMENTS.DELETED,0)<>1
AND PM.Patient_GlobalId='''+ #GLOBALPATIENTACCOUNT + ''' AND PM.PRACTICE_CODE<>''' + #practice_code + ''' and DOCUMENTS.confidential<>1'
IF(#dateFrom <> 'null')
BEGIN
SET #sqlstr = #sqlstr + 'and ISNULL(CONVERT(VARCHAR,PATIENT_DOCUMENTS.CREATED_DTAE,101),'''') >= ISNULL(CONVERT(VARCHAR,''' + #dateFrom + ''',101),'''') and ISNULL(CONVERT(VARCHAR,PATIENT_DOCUMENTS.CREATED_DTAE,101),'''')<=ISNULL(CONVERT(VARCHAR,''' + #dateTo + ''',101),'''')'
END
IF (#startFrom <> 'null')
BEGIN
SET #sqlstr = #sqlstr + 'CONVERT(VARCHAR,PATIENT_DOCUMENTS.CONTENT_START_DATE,101) >= CONVERT(VARCHAR,''' + #startFrom + ''',101) and CONVERT(VARCHAR,PATIENT_DOCUMENTS.CONTENT_START_DATE,101)<=CONVERT(VARCHAR,''' + #startTo + ''',101)'
END
SET #sqlstr = #sqlstr + ') PatDocTable order by PRACTICE_CODE,CREATED_DTAE desc '
EXECUTE sp_executesql #statement = #sqlstr
PRINT #sqlstr
It gives me error Procedure expects parameter '#statement' of type 'ntext/nchar/nvarchar',but when i change datatype #sqlstr AS nvarchar (max),error omitted,but it trancuate my query.Kindly help me to figure it out?
sp_executesql takes NVARCHAR as parameter not VARCHAR, change varchar(max) to nvarchar(max) will fix the problem.
DECLARE #sqlstr AS nvarchar (max)
Here is one of the possible solution:
Convert your #sqlstr to varchar(max) and instead of EXECUTE sp_executesql, use EXECUTE(#strsql).In this fashion you got your complete print query and it will also execute your query.Hope that it helps.
Declare variable as a nvarchar type when you are using command exec sp_executesql.Above you used varchar for #sqlstr variable.
Eg.
Declare #Sqlstr nvarchar(max)
SET #Sqlstr='...Your dynamic query...'
exec sp_executesql #Sqlstr
Note:Dont use brackets in exec command
using sp_executesql is very good option for this type of queries and it is not very friendly for sql injection.
ex:
DECLARE #sqlString nvarchar(500);
DECLARE #paramDefinition nvarchar(500);
DECLARE #sid int = 12546;
SET #sqlString = 'select * from SameTableBase S Where S.Id = #id';
SET #paramDefinition = N'#id int';
EXECUTE sp_executesql #sqlString , #paramDefinition , #id = #sid

Must declare the table variable "#temp"

I create a stored procedure in which I am using a dynamic query. The query will get data from my query and insert into my temp table and in last I select data from #temp table.
This is my stored procedure:
ALTER PROCEDURE uspSearchEmployee
#Name varchar(50)= null,
#EmpNumber varchar(50)=null,
#Location Varchar(50)=null,
#position varchar(50)=null,
#partialmatch bit ,
#partialmatch2 bit,
#partialmatch3 bit
AS
BEGIN
declare #wheresql varchar(max)
if (#Name is not null)
BEGIN
if(#partialmatch=1)
set #wheresql=' where EmpName like ''%'+ #Name + ''''
else
set #wheresql=' where EmpName = ' +#Name
END
IF(#Name is null AND #EmpNumber IS NOT NULL )
BEGIN
if(#partialmatch2=1)
set #wheresql=' where EmployeeNum like ''%' + #EmpNumber + ''''
else
set #wheresql=' where EmployeeNum = '+#EmpNumber
END
IF(#Name is NOT null AND #EmpNumber IS NOT NULL )
BEGIN
if(#partialmatch2=1)
set #wheresql=#wheresql+' AND EmployeeNum like ''%'+#EmpNumber +''''
else
set #wheresql=#wheresql+' AND EmployeeNum ='+#EmpNumber
END
--3rd case
IF(#Name is null AND #EmpNumber IS NULL AND #Location IS NOT NULL )
BEGIN
if(#partialmatch3=1)
set #wheresql=' where Location like ''%' +#Location + ''''
else
set #wheresql=' where Location = ' +#Location
END
IF((#Name is NOT null OR #EmpNumber IS NOT NULL) AND #Location IS NOT NULL )
BEGIN
if(#partialmatch3=1)
set #wheresql=#wheresql+ ' AND Location like ''%'+#EmpNumber + ''''
else
set #wheresql=#wheresql+' AND Location ='+#EmpNumber
END
IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp
(
EmpId int,
EmpName varchar(40),
Location varchar(50)null,
City varchar(40)null,
STPROV varchar(15)null,
EmergencyPhone varchar(50)null,
ZIPPOSTAL varchar(10) null,
Home_Phone varchar(25)null,
Country varchar(25) null,
Department varchar(50) null,
Position varchar(50) null,
WorkCompCode varchar(50) null,
Active bit null
)
set #wheresql=' INSERT INTO #temp
SELECT EmpId,EmpName,Location,City,STPROV,EmergencyPhone,ZIPPOSTAL,Home_Phone,Country,Department
Position,WorkCompCode,Active
from employee ' + ISNULL(#wheresql, '')
PRINT #wheresql
EXECUTE sys.sp_executesql #wheresql
Select * FROM #temp
END
When I execute I get error message
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "#temp".
What am I doing wrong in this query?
Regards
Try to use temporary tables -
ALTER PROCEDURE dbo.uspSearchEmployee
#Name VARCHAR(50) = NULL,
#EmpNumber VARCHAR(50) = NULL,
#Location VARCHAR(50) = NULL,
#position VARCHAR(50) = NULL,
#partialmatch BIT,
#partialmatch2 BIT,
#partialmatch3 BIT
AS BEGIN
SET NOCOUNT ON;
DECLARE #SQL NVARCHAR(MAX)
IF #Name IS NOT NULL
BEGIN
IF (#partialmatch = 1)
SET #SQL = ' WHERE EmpName LIKE ''%' + #Name + ''''
ELSE
SET #SQL = ' WHERE EmpName = ' + #Name
END
IF #Name IS NULL AND #EmpNumber IS NOT NULL
BEGIN
IF (#partialmatch2 = 1)
SET #SQL = ' WHERE EmployeeNum LIKE ''%' + #EmpNumber + ''''
ELSE
SET #SQL = ' WHERE EmployeeNum = ' + #EmpNumber
IF (#partialmatch2 = 1)
SET #SQL = #SQL + ' AND EmployeeNum LIKE ''%' + #EmpNumber + ''''
ELSE
SET #SQL = #SQL + ' AND EmployeeNum =' + #EmpNumber
END
IF #Name IS NULL AND #EmpNumber IS NULL AND #Location IS NOT NULL
BEGIN
IF (#partialmatch3 = 1)
SET #SQL = ' WHERE Location LIKE ''%' + #Location + ''''
ELSE
SET #SQL = ' WHERE Location = ' + #Location
END
IF (#Name IS NOT NULL OR #EmpNumber IS NOT NULL) AND #Location IS NOT NULL
BEGIN
IF (#partialmatch3 = 1)
SET #SQL = #SQL + ' AND Location LIKE ''%' + #EmpNumber + ''''
ELSE
SET #SQL = #SQL + ' AND Location =' + #EmpNumber
END
IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp
(
EmpId INT,
EmpName VARCHAR(40),
Location VARCHAR(50) NULL,
City VARCHAR(40) NULL,
STPROV VARCHAR(15) NULL,
EmergencyPhone VARCHAR(50) NULL,
ZIPPOSTAL VARCHAR(10) NULL,
Home_Phone VARCHAR(25) NULL,
Country VARCHAR(25) NULL,
Department VARCHAR(50) NULL,
Position VARCHAR(50) NULL,
WorkCompCode VARCHAR(50) NULL,
Active BIT NULL
)
SET #SQL = '
INSERT INTO #temp
SELECT EmpId,EmpName,Location,City,STPROV,EmergencyPhone,ZIPPOSTAL,Home_Phone,Country,Department,Position,WorkCompCode,Active
FROM dbo.employee ' + ISNULL(#SQL, '')
PRINT #SQL
EXEC sys.sp_executesql #SQL
SELECT * FROM #temp
END

incorrect syntax near '*'

I was trying to execute one of my Stored procedure but i am getting an syntax error and i am unable to understand why.
here is the sproc:
ALTER PROCEDURE [dbo].[HotlinePlusAdministration_ArticleMigrator]
#ArticleKey AS INT,
--#CategoryID AS INT,
--#Title AS Varchar(200),
--#ArticleDate AS datetime,
#DestLinkServer AS VARCHAR(50),
#UserID AS VARCHAR(8),
#ReturnMsg AS VARCHAR(1000) OUTPUT
AS
BEGIN
DECLARE #Query AS NVARCHAR(4000)
DECLARE #Log AS VARCHAR(8000)
DECLARE #ArticleID as int
DECLARE #NewArticleID as int
DECLARE #ArticleKeyExists as int
DECLARE #Title as varchar(200)
DECLARE #CategoryID as INT
DECLARE #ArticleDate as varchar(30)
DECLARE #ParmDefinition nvarchar(500);
SET XACT_ABORT ON -- Required for nested transaction
BEGIN TRAN
-- Check if ArticleID exists in Destination Server
SET #Query = N' SELECT #ArticleKeyExists = COUNT(*)
FROM ' + #DestLinkServer + '.HL2_61.dbo.Article' + ' where ArticleKey = ' + str(#ArticleKey)
SET #ParmDefinition = N'#ArticleKey int, #ArticleKeyExists int OUTPUT';
EXECUTE sp_executesql #Query , #ParmDefinition, #ArticleKey , #ArticleKeyExists OUTPUT;
IF ##ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
SET #ReturnMsg = #Log + '<span style="color:red;">ERROR: <br></span>'
RETURN -1
END
--Delete existing Articles for select page
set #Query = 'DELETE FROM ' + #DestLinkServer +
'.HL2_61.dbo.Article ' +
'WHERE ArticleKey = ' + CONVERT(VARCHAR, #ArticleKey)
--'WHERE CategoryID = ' + CONVERT(VARCHAR, #CategoryID) + ' and Title = ''' + #Title + ''' and ArticleDate = ''' + #ArticleDate + ''''
Print #Query
EXEC(#Query)
when i am trying to execute it i am getting an error here:
SELECT #ArticleKeyExists = COUNT(*)
FROM BRWSQLDC.HL2_61.dbo.Article where ArticleKey = 1591276581
Can some body please help me on this,
Thanks.
SET #ArticleKeyExists = (SELECT COUNT(*) FROM BRWSQLDC.HL2_61.dbo.Article where ArticleKey = 1591276581)

Resources