How to use IF condition for column selection in sql server 2008 R2 - sql

In my project I want data from same table in 2 different manner.
1) Only 5 fields
2) Only 10 fields
And I am getting the data through Stored Procedure for each one. But I want that if possible I should make one procedure.
So, can I put if condition for selecting column?
For example, if I pass parameter as "Less", it will get data of only 5 columns and if I pass parameter as "More", it will get data of 10 columns. I can have two SELECT statement in procedure based on condition(that I have already done) but I want to make it One SELECT statement. Is it possible?

No, is it not possible to have a single SELECT statement.
But then, you can do this:
if #ColumnList = 'less'
begin
call storedProc_returning5Columns
end
else
begin
call storedProc_returning10Columns
end
[Another option is to use Dynamic TSQL]

This isn't possible.
Your current solution of having an IF statement in the stored procedure is the best approach.

Related

I have a db2 stored procedure returning 2 values, how do i use this procedure in a select statement?

I have a DB2 stored procedure (my_proc) that returns two values. I want these two values to be displayed as two different columns.
Is the following correct way to use it?
select my_proc(parameter1,parameter2,parameter3) as column1,column2;
Please guide as I am learning DB2 and new to stored procedure.
Consider if it is possible or not for you to convert your stored procedure to a UDTF (user defined table function). This can be selected from and joined to and works very similar to a stored procedure.
https://www.itjungle.com/2016/06/14/fhg061416-story03/

Selecting from the result set of a DB2 stored procedure

I have a stored procedure which returns multiple records (the SP cannot be changed, I need to work with what I have). I'd like to do a DB2 select statement from Shell script that selects one record based on a combination of column data like the following:
select a.description_column from (call my_stored_proc) a where a.name_column='name_filter' and a.value_column='value_filter';
The columns description_column, name_column and value_column exist in the result set of the SP. I get a SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2 error. As I need to sort it out from a Shell script and I only have read access to the DB, I can't create additional tables for this.
You can't select from SP.
But you can from a Table-Function.
SELECT ... FROM TABLE(<table-function(param1, param2, ..., paramN))) as t WHERE ....
So, easiest way is to ask the DBA to create a table-function based in the source SP.
Good luck

How do I use case statement to aid altering another field?

I have a hopefully pretty simple question here. I'm converting some Access SQL script into Server Management Studio 2008.
Currently the Access script shows the following line of code:
IIf([rsosourceID]) IN (254,360,446),"MoneySavingExpert" as SourceName
Basically it's creating a temporary table with four columns, if the fields match say those 3 numbers on the left then it will populate a fourth column with their new name. (To be used for grouping in a later report)
How can I do something simillar in SQL? I've tried using a Case statement but to my knowledge that will only alter the field you're looking against.
In this case I need to look at one field then using it's results alter another, any ideas?
A case statement can return a new column using the value of any other column(s):
SELECT rsoSourceID,
rsoDescription,
rsoCategory,
case when rsoSourceID in (254,360,446)
then 'MoneySavingExpert'
else null end as SourceName
FROM TableName

Data Manipulation Language with and Without GO statement [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the use of GO in SQL Server Management Studio?
I have some Data Manipulation Language like below.
Case - 1 Without GO
Update Table
Set Columns = 'Value'
Where Id = 1
Update Table
Set Columns = 'Value'
Where Id = 2
Case - 2 With GO
Update Table
Set Columns = 'Value'
Where Id = 1
GO
Update Table
Set Columns = 'Value'
Where Id = 2
Query
Which should be preferred and why ?
The only difference - that the 1st query runs in one turn since 2nd - in tho turns. GO delimiter is not a server command, it is just a batch separator, which should be handled by client.
This means - that 1st query performs better because of 1-turn query
Refer below url it may help you.
http://msdn.microsoft.com/en-us/library/ms188037.aspx which states:
GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor.
SQL Server utilities interpret GO as a signal that they should send
the current batch of Transact-SQL statements to an instance of SQL
Server. The current batch of statements is composed of all statements
entered since the last GO, or since the start of the ad hoc session or
script if this is the first GO.
A Transact-SQL statement cannot occupy the same line as a GO command.
However, the line can contain comments.
Users must follow the rules for batches. For example, any execution of
a stored procedure after the first statement in a batch must include
the EXECUTE keyword. The scope of local (user-defined) variables is
limited to a batch, and cannot be referenced after a GO command.
I got the differences in terms of Stored Procedure.
Case 1 - When Select 2 is not the part and explicitly seperated from this stored procedure
Create proc abc
as
select 1
GO
select 2
Case 2 - When Select 2 is not the part of stored Procedure and by mistake became the part of stored procedure due to absence of GO
Create proc abc
as
select 1
select 2
In case 2 , Definition of the language becomes following.
Create proc abc
as
select 1
select 2

SQL Server Stored Procedure - Use Row Count in Select query

My stored procedure (SQL Server 2005) returns a dataset where one field depends, among other things, on the number of rows returned by the query. I can make a simplified first query that allows me to get ##ROWCOUNT but, in that case, the procedure returns the two sets, which is not what I want.
I tried putting the first query in a WITH statement but haven't found the syntax to extract the row count and put it in a variable that I could use in the second query. An alternative would be to get ##ROWCOUNT from the first query and tell the procedure to return only the result of the second query.
There are probably better ways to do that but my expertise in SQL is quite limited...
Thanks for any help!
Is this what you're looking for? If not, could you please describe your problem in more details (perhaps, with code snippets)
alter procedure ComplicatedStoredProcedure as
begin
declare #lastQueryRowCount int
-- Storing the number of rows returned by the first query into a variable.
select #lastQueryRowCount =
-- First resultset (not seen by caller).
(select count(*) from A where ID > 100)
-- Second resultset. This will be the actual result returned from the SP.
select * from B where SomeDependentField > #lastQueryRowCount
end