Update column information - sql

I am trying to update information in the column Mgrstat to a 3 and would like to mass enter the information. As it is I have to use "=" and enter each AppID individually but I would rather enter several at once. The query below shows my attempt using "in", which didn't work either. I get "Incorrect syntax near the keword 'in'".
Any ideas? Thanks everyone!
declare #appid as int
declare #mgrstat as int
set #appid in ('10995',
'11201',
'9523',
'9558',
'9666',
'10069',
'10547',
'10548',
'9702',
'10698',
'9754',
'10161',
'10162',
'11240',
'11241',
'9553',
'10848',
'10667',
'9383',
'10709',
'9696',
'10053',
'10702')
set #mgrstat = 3
update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = #mgrstat
FROM [Compensation].[dbo].[dev_RPT_Approval]
where #appid = App_Id
select *
from [Compensation].[dbo].[dev_RPT_Approval]
where #appid = App_Id

This is the SQL you Need:
update dev_RPT_Approval set Mgr_Stat=3
where designation
in ('10995',
'11201',
'9523',
'9558',
'9666',
'10069',
'10547',
'10548',
'9702',
'10698',
'9754',
'10161',
'10162',
'11240',
'11241',
'9553',
'10848',
'10667',
'9383',
'10709',
'9696',
'10053',
'10702')

If i'm understanding correctly, and you want all mgr_stats to be 3 where the app_id is in the list provided in your question, then you could do this a few ways:
update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = 3
where app_id in (
'10995',
'11201',
'9523',
'9558',
'9666',
'10069',
'10547',
'10548',
'9702',
'10698',
'9754',
'10161',
'10162',
'11240',
'11241',
'9553',
'10848',
'10667',
'9383',
'10709',
'9696',
'10053',
'10702'
)
or (sql server using table variable)
declare #ids table (id varchar(50))
insert into #ids (id)
select '10995'
union all select '11201'
union all select '9523'
union all select '9558'
union all select '9666'
union all select '10069'
union all select '10547'
union all select '10548'
union all select '9702'
union all select '10698'
union all select '9754'
union all select '10161'
union all select '10162'
union all select '11240'
union all select '11241'
union all select '9553'
union all select '10848'
union all select '10667'
union all select '9383'
union all select '10709'
union all select '9696'
union all select '10053'
union all select '10702'
update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = 3
from [Compensation].[dbo].[dev_RPT_Approval] t
inner join #ids i on t.app_id = i.id
A few things to note about the code you had posted:
declare #appid as int
set #appId in ...
A few things with this - #appId is declared as an integer, meaning it is a scalar value (cannot be a set) - for sets of values, you can use a table variable as I did in my second example of how to accomplish your question.
Additionally, because you variable as an int, I'm assuming your ID is of type int, the quotes are not needed.
Instead of:
where app_id in (
'10995',
....
)
you can do:
where app_id in (
10995,
....
)

can you try this?
Might work for you dude. Here you are passing multiple values using "=" instead "IN"
update [Compensation].[dbo].[dev_RPT_Approval]
set Mgr_Stat = #mgrstat
FROM [Compensation].[dbo].[dev_RPT_Approval]
where App_Id IN(#appid)

Related

Passing Value to parameter in stored procedure

I have a list of value which is more than 500, and every time I have to pass those value to the below stored procedure. Will it be possible to call those value dynamically?
RDBMS: SQL Server 2014
small set of #value example below.
declare #valuetable table
(
value varchar(50)
)
insert #valuetable
select video union
select audio union
select hayward union
select abott union
select gsk
Code:
DECLARE #value VARCHAR(24) SET #value = 'video'
DECLARE #DAYS INT SET #DAYS = -30
SELECT * INTO #XTP1 FROM (
SELECT DISTINCT 'START' AS DTT, DATEADD(D,#DAYS,DATEACTIONED) AS DT FROM NEWREPORTS
WHERE value = #value
UNION
SELECT DISTINCT 'CHANGE' AS DTT, DATEACTIONED AS DT FROM NEWREPORTS
WHERE value = #value
)r
Thanks
So what you need to do is to add one column to #XTP1 to store the value name. Then you can do:
declare #valuetable table
(
value varchar(50)
)
insert #valuetable
select video union
select audio union
select hayward union
select abott union
select gsk
SELECT * INTO #XTP1 FROM (
SELECT DISTINCT v.value, 'START' AS DTT, DATEADD(D,#DAYS,DATEACTIONED) AS DT
FROM NEWREPORTS n INNER JOIN #valuetable v ON n.value = v.value
UNION
SELECT DISTINCT v.value, 'CHANGE' AS DTT, DATEACTIONED AS DT
FROM NEWREPORTS n INNER JOIN #valuetable v ON n.value = v.value
)r
Now you can retrieve everything from #XTP1 in one go. You will probably want to include "value" in your ORDER BY.

Selecting / Searching Multiple values from multiple tables , returning only the matched cases

Dears Please help me pass multiple CPs to find the Process Ids from any one of the following table .
I want to do something like this :
declare #CP varchar(30)
Set #CP ='684980','123123','456456'
select ESuser.tb_pt_servicerequest.process_instance_id, ESuser.tb_pt_servicerequest.cpno
from
ESuser.tb_pt_servicerequest where ESuser.tb_pt_servicerequest.cpno in (#CP)
union all
select ESuser.tb_pt_additionalcp.process_instance_id, ESuser.tb_pt_additionalcp.cpno
from
ESuser.tb_pt_additionalcp where ESuser.tb_pt_additionalcp.cpno in (#CP)
OR like this
select * from XXXX, WWW, MMMM, KKK where CP_NO in ('123123','123321','123567')
Using this ,
But here I am unable to pass multiple cp number to get the returns :
Now in this variable i can pass one value like '123123' and can get the results ,
but if i change the query to like in ('123123','123321') instead of =('123123'),
I could not get the results . :(
declare #CP varchar(10)
Set #CP ='684980 '
select ESuser.tb_pt_servicerequest.process_instance_id, ESuser.tb_pt_servicerequest.cpno
from
ESuser.tb_pt_servicerequest where ESuser.tb_pt_servicerequest.cpno = #CP
union all
select ESuser.tb_pt_additionalcp.process_instance_id, ESuser.tb_pt_additionalcp.cpno
from
ESuser.tb_pt_additionalcp where ESuser.tb_pt_additionalcp.cpno = #CP
union all
select ESuser.tb_pt_new_addtionalcp.process_instance_id, ESuser.tb_pt_new_addtionalcp.cpno
from
ESuser.tb_pt_new_addtionalcp where ESuser.tb_pt_new_addtionalcp.cpno = #CP
union all
select ESuser.tb_pt_vip_service_request.process_instance_id,
ESuser.tb_pt_vip_service_request.cpno
from
ESuser.tb_pt_vip_service_request where ESuser.tb_pt_vip_service_request.cpno = #CP
union all
select
ESuser.tb_pt_vip_additional_cp.process_instance_id,ESuser.tb_pt_vip_additional_cp.cpno
from
ESuser.tb_pt_vip_additional_cp where ESuser.tb_pt_vip_additional_cp.cpno = #CP
union all
select ESuser.tb_pt_servicerequestbypass.process_instance_id, ESuser.tb_pt_servicerequestbypass.cpno
from
ESuser.tb_pt_servicerequestbypass where ESuser.tb_pt_servicerequestbypass.cpno = #CP
You have to use a table variable. This is how you can declare it and populate it:
DECLARE #CP TABLE (CP_ID VARCHAR(10))
INSERT INTO #CP VALUES ('123123'),('123321'),('123567')
And this is how you can use it in your query:
SELECT ESuser.tb_pt_servicerequest.process_instance_id,
ESuser.tb_pt_servicerequest.cpno
FROM ESuser.tb_pt_servicerequest
WHERE ESuser.tb_pt_servicerequest.cpno IN (SELECT CP_ID FROM #CP)

Collation Conflict can not solve in SQL server

This is my query:
SELECT
CASE WHEN (
select Count(*) From (
select * from [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA
union
select * from [vmslcsql11].[HSRTest].dbo.External_Member_data) as t
)
<>
(
Select count(*) From [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA
)
THEN 'Data is not Identical'
ELSE 'Date is identical'
END AS RowCountResult
I am getting Following error
cannot resolve the collation conflict between sql_latin1_general_cp1_ci_as and sql_latin1_general_cp1_ci_ai
I know that error is because collation mismatch for one of the my column external Data with nvarchar type
I can solve the error by using DefaultCollation. As much as I understood DefaultCollation is used only with column_name. I am using * here. I don't know how to solve this error with scenario
Please find below the solution for your problem.
Change the table names with your tables.
declare #count as int
set #count =
(Select Count(*) from
(select * from Broker
union
select * from Broker) as t)
SELECT
CASE WHEN
(
#count <> (select count(*) from Broker)
)
THEN 'Data is not Identical'
ELSE 'Date is identical'
END AS RowCountResult
Your issue is related to using select *. Always avoid using this.
You were trying to union two columns with different collations. You wouldn't know which ones though because you were using select *
The statement below doesn't use select * at all and is likely to be much faster as it should work out the count on the remote server and return only three rows in total. Also it does not use union which is an expensive operation.
The important question is: did you mean to use union instead of union all ?
SELECT
RowCount1,
RowCount2,
CASE WHEN RowCount1 <> RowCount2
THEN 'Data is not Identical'
ELSE 'Date is identical'
END AS RowCountResult
FROM
(
SELECT
(select Count(*) From mslccard08.[carekey].dbo.EXTERNAL_MEMBER_DATA)
+
(select Count(*) from [vmslcsql11].[HSRTest].dbo.External_Member_data)
As RowCount1,
(
Select count(*) From [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA
)
As RowCount2
) As SubTable
SELECT
CASE WHEN (
select Count(1) From (
select field1 COLLATE sql_latin1_general_cp1_ci_as field1, field2 COLLATE sql_latin1_general_cp1_ci_as field2 from [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA
union
select field1 COLLATE sql_latin1_general_cp1_ci_as field1, field2 COLLATE sql_latin1_general_cp1_ci_as field2 from [vmslcsql11].[HSRTest].dbo.External_Member_data
) t)
<>
(
Select count(*) From [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA
)
THEN 'Data is not Identical'
ELSE 'Date is identical'
END AS RowCountResult
You need to correct the field names, it should work in union.
Changing collation with * does not seem to be possible.
EDIT:Temp Table Option to change COLLATE when using * in select
Create a temp table for first statement of the union then upsert records from second statement of the union and use this temp table for getting both table's count, refer code example below.
create table t1(name varchar(10) COLLATE French_CI_AS);
create table t2(name varchar(10) COLLATE Latin1_General_CI_AS);
insert into t1 values ('`ffffn1');
insert into t2 values('general');
select * into #t from t1; -- First table of union
Insert into #t select * from t2; -- second table of union
select count(*) from #t; -- working
------------------------------------------
So your query will change to;
select * into #EXTERNAL_MEMBER_DATA from [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA ; -- Create temp table having carekey records
Insert into #EXTERNAL_MEMBER_DATA select * from [vmslcsql11].[HSRTest].dbo.External_Member_data; -- add records from HSRTest
-- Use the temp table in query
SELECT CASE WHEN (select Count(1) From #EXTERNAL_MEMBER_DATA )
<>
(Select count(*) From [mslccard08].[carekey].dbo.EXTERNAL_MEMBER_DATA)
THEN 'Data is not Identical'
ELSE 'Date is identical'
END AS RowCountResult

Select with IN and Like

I have a very interesting problem. I have an SSRS report with a multiple select drop down.
The drop down allows to select more than one value, or all values.
All values is not the problem.
The problem is 1 or the combination of more than 1 option
When I select in the drop down 'AAA' it should return 3 values: 'AAA','AAA 1','AAA 2'
Right now is only returning 1 value.
QUESTION:
How can make the IN statement work like a LIKE?
The Drop down select
SELECT '(All)' AS team, '(All)' AS Descr
UNION ALL
SELECT 'AAA' , 'AAA'
UNION ALL
SELECT 'BBB' , 'BBB'
Table Mytable
ColumnA Varchar(5)
Values for ColumnA
'AAA'
'AAA 1'
'AAA 2'
'BBB'
'BBB 1'
'BBB 2'
SELECT * FROM Mytable
WHERE ColumnA IN (SELECT * FROM SplitListString(#Team, ',')))
Split function
CREATE FUNCTION [dbo].[SplitListString]
(#InputString NVARCHAR(max), #SplitChar CHAR(1))
RETURNS #ValuesList TABLE
(
param NVARCHAR(MAX)
)
AS
BEGIN
DECLARE #ListValue NVARCHAR(max)
DECLARE #TmpString NVARCHAR(max)
DECLARE #PosSeparator INT
DECLARE #EndValues BIT
SET #TmpString = LTRIM(RTRIM(#InputString));
SET #EndValues = 0
WHILE (#EndValues = 0) BEGIN
SET #PosSeparator = CHARINDEX(#SplitChar, #TmpString)
IF (#PosSeparator) > 1 BEGIN
SELECT #ListValue = LTRIM(RTRIM(SUBSTRING(#TmpString, 1, #PosSeparator -1 )))
END
ELSE BEGIN
SELECT #ListValue = LTRIM(RTRIM(#TmpString))
SET #EndValues = 1
END
IF LEN(#ListValue) > 0 BEGIN
INSERT INTO #ValuesList
SELECT #ListValue
END
SET #TmpString = LTRIM(RTRIM(SUBSTRING(#TmpString, #PosSeparator + 1, LEN(#TmpString) - #PosSeparator)))
END
RETURN
END
You can't. But, you can make the like work like the like:
select *
from mytable t join
SplitListString(#Team, ',') s
on t.ColumnA like '%'+s.param+'%'
That is, move the split list to an explicit join. Replace with the actual column name returned by the function, and use the like function.
Or, if you prefer:
select *
from mytable t cross join
SplitListString(#Team, ',') s
where t.ColumnA like '%'+s.param+'%'
The two versions are equivalent and should produce the same execution plan.
Better approach would be to have a TeamsTable (teamID, teamName, ...) and teamMembersTable (teamMemberID, teamID, teamMemberDetails, ...).
Then you an build your dropdown list as
SELECT ... FROM TeamsTable ...;
and
SELECT ... FROM teamMembersTable WHERE teamID IN (valueFromYourDropDown);
Or you can just store your teamID or teamName (or both) in your (equivalent of) teamMembersTable
You're not going to get IN to work the same as LIKE without a lot of work. You could do something like this though (and it would be nice to see some of your actual data though so we could give better solutions):
SELECT *
FROM table
WHERE LEFT(field,3) IN #Parameter
If you'd like better performance, create a code field on your table and update it like this:
UPDATE table
SET codeField = LEFT(field,3)
Then just add an index on that field and run this query to get your results:
SELECT *
FROM table
WHERE codeField IN #Parameter

T-Sql count string sequences over multiple rows

How can I find subsets of data over multiple rows in sql?
I want to count the number of occurrences of a string (or number) before another string is found and then count the number of times this string occurs before another one is found.
All these strings can be in random order.
This is what I want to achieve:
I have one table with one column (columnx) with data like this:
A
A
B
C
A
B
B
The result I want from the query should be like this:
2 A
1 B
1 C
1 A
2 B
Is this even possible in sql or would it be easier just to write a little C# app to do this?
Since, as per your comment, you can add a column that will unambiguously define the order in which the columnx values go, you can try the following query (provided the SQL product you are using supports CTEs and ranking functions):
WITH marked AS (
SELECT
columnx,
sortcolumn,
grp = ROW_NUMBER() OVER ( ORDER BY sortcolumn)
- ROW_NUMBER() OVER (PARTITION BY columnx ORDER BY sortcolumn)
FROM data
)
SELECT
columnx,
COUNT(*)
FROM marked
GROUP BY
columnx,
grp
ORDER BY
MIN(sortcolumn)
;
You can see the method in work on SQL Fiddle.
If sortcolumn is an auto-increment integer column that is guaranteed to have no gaps, you can replace the first ROW_NUMBER() expression with just sortcolumn. But, I guess, that cannot be guaranteed in general. Besides, you might indeed want to sort on a timestamp instead of an integer.
I dont think you can do it with a single select.
You can use AdventureWorks cursor:
create table my_Strings
(
my_string varchar(50)
)
insert into my_strings values('A'),('A'),('B'),('C'),('A'),('B'),('B') -- this method will only work on SQL Server 2008
--select my_String from my_strings
declare #temp_result table(
string varchar(50),
nr int)
declare #myString varchar(50)
declare #myLastString varchar(50)
declare #nr int
set #myLastString='A' --set this with the value of your FIRST string on the table
set #nr=0
DECLARE string_cursor CURSOR
FOR
SELECT my_string as aux_column FROM my_strings
OPEN string_cursor
FETCH NEXT FROM string_cursor into #myString
WHILE ##FETCH_STATUS = 0 BEGIN
if (#myString = #myLastString) begin
set #nr=#nr+1
set #myLastString=#myString
end else begin
insert into #temp_result values (#myLastString, #nr)
set #myLastString=#myString
set #nr=1
end
FETCH NEXT FROM string_cursor into #myString
END
insert into #temp_result values (#myLastString, #nr)
CLOSE string_cursor;
DEALLOCATE string_cursor;
select * from #temp_result
Result:
A 2
B 1
C 1
A 1
B 2
Try this :
;with sample as (
select 'A' as columnx
union all
select 'A'
union all
select 'B'
union all
select 'C'
union all
select 'A'
union all
select 'B'
union all
select 'B'
), data
as (
select columnx,
Row_Number() over(order by (select 0)) id
from sample
) , CTE as (
select * ,
Row_Number() over(order by (select 0)) rno from data
) , result as (
SELECT d.*
, ( SELECT MAX(ID)
FROM CTE c
WHERE NOT EXISTS (SELECT * FROM CTE
WHERE rno = c.rno-1 and columnx = c.columnx)
AND c.ID <= d.ID) AS g
FROM data d
)
SELECT columnx,
COUNT(1) cnt
FROM result
GROUP BY columnx,
g
Result :
columnx cnt
A 2
B 1
C 1
A 1
B 2