SQL Server: If, Else Select Statements to populate a grid - sql

I'm trying to populate a data-grid with information from my table. One of the fields in this table is Active basically used as a flag set to either True or False.
I have a query on my website that checks if the currently logged-in user's is an administrator or not.
Using that in a TOKEN I can input [LSX:IsAdmin] that will return either true or false.
All data in my table that is NOT flagged Active=True i'd like to display only to Administrators which is what my TOKEN is for.
I'm having some difficulties with my query below to achieve that.
IF('LSX:IsAdmin'='True')
SELECT * FROM SaleEvents
ELSE
SELECT * FROM SaleEvents WHERE Active = True
Error Message Invalid object name 'SaleEvents'.

First picture shows how to find out schema name (CIS, dbo, TestCars, TestDoc, TestMoney). 'dbo' schema usually exists and default.
However, you can change default schema for user (second picture).
You do not specify schema name in your query, that's why default schema is used.
To specify a schema name you should add it before table name
SELECT * FROM <your schema>.SaleEvents WHERE Active = 1

Related

Update a specific column with a specific value

I have a table with 3 columns: username, password and ID.Every password and username have a specific ID. Table name is "Account" for example.
I want to update a password with a specific ID. I have tried:
UPDATE Account SET password = "newPassword"
where id = 1
However, it does not work. It will complain that "newPassword" is not a valid column name. I try my queries in SQL management studio.
Use single quotes for string literals in MS SQL Server:
UPDATE Account
SET password = 'newPassword'
WHERE id = 1;
Double quotes in SQL are generally reserved for database objects, such as table and column names (sometimes called identifiers). Your current update is attempting to assign the password column to a column named newPassword. This column doesn't exist, hence you are getting an error.

DB2 SQL - Set variable value based on user input

In DB2-LUW SQL, I can write a query to do this:
select * from customers
&input
When a user runs that they'll be prompted for input and can type:
where name = 'Bill'
And the query that actually gets executed will be:
select * from customers
where name = 'Bill'
I am trying to figure out a way to vary the value of &input to alter the query based on more basic user input - so they won't need to type in where name = 'Bill'.
For example, the user could be prompted to enter either YesBill or NoBill and depending on what they've entered, the value of &input will be set and the executed query would be either:
select * from customers
where name = 'Bill'
or
select * from customers
where name <> 'Bill'
The example is meaningless, I'm mostly wondering if it's possible to vary the value of &input without forcing the user to type in SQL code.
Hope that makes sense. Thanks for any help!
The answer to this question really depends on the SQL client that you are using.
For example, if you use DBeaver, then you can simply use : to prefix your variable names and then when executing the query, you will get a prompt to window to enter your variable names.
Other query tools may, nor may not, provide a similar capability

SSIS- if data exists based on a select statement, then send the data via email else nothing

I want to create a package where I want to see if the data exists in the select statement, then send an email along with the data(doesn't matter in which form- excel/text file) else nothing.
Could you please suggest what tasks I need to use in the package?
Basically doing T-sql to check if location changed of employees, if so, notify via email and providing the data that changed in the email.
Thanks in advance!
You have to use an Execute SQL Task, to check if Data Exists and assign a Value to a Result Set.
Example
IF EXISTS(SELECT 1 FROM TABLE)
SELECT 1 AS Result
ELSE
SELECT 0 AS Result
Then you should specify that the Execute SQL store the returned value in a single Row result set For more info refer to this link
Then you should add precedence constraint with the following expression (assuming that #[User:ResultValue] is the variable where ResultSet is stored)
#[User:ResultValue] == 1
And You can refer to the following question for detailed answer about sending sql query result as mail
How do I send the result set from a query as an attachment in an email using SSIS?

How to find schema name in Oracle ? when you are connected in sql session using read only user

I am connected to a oracle database with a read only user and i used service name while Setting up connection in sql developer hence i dont know SID ( schema ).
How can i find out schema name which i am connected to ?
I am looking for this because i want to generate ER diagram and in that process at one step it asks to select schema. When i tried to select my user name , i dint get any tables as i guess all tables are mapped with schema user.
Edit: I got my answer partially by the below sql Frank provided in comment , it gave me owner name which is schema in my case. But I am not sure if it is generic solution applicable for all cases.
select owner, table_name from all_tables.
Edit: I think above sql is correct solution in all cases because schema is owner of all db objects. So either i get schema or owner both are same. Earlier my understanding about schema was not correct and i gone through another question and found schema is also a user.
Frank/a_horse_with_no_name Put this in answer so that i can accept it.
Call SYS_CONTEXT to get the current schema. From Ask Tom "How to get current schema:
select sys_context( 'userenv', 'current_schema' ) from dual;
To create a read-only user, you have to setup a different user than the one owning the tables you want to access.
If you just create the user and grant SELECT permission to the read-only user, you'll need to prepend the schema name to each table name. To avoid this, you have basically two options:
Set the current schema in your session:
ALTER SESSION SET CURRENT_SCHEMA=XYZ
Create synonyms for all tables:
CREATE SYNONYM READER_USER.TABLE1 FOR XYZ.TABLE1
So if you haven't been told the name of the owner schema, you basically have three options. The last one should always work:
Query the current schema setting:
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL
List your synonyms:
SELECT * FROM ALL_SYNONYMS WHERE OWNER = USER
Investigate all tables (with the exception of the some well-known standard schemas):
SELECT * FROM ALL_TABLES WHERE OWNER NOT IN ('SYS', 'SYSTEM', 'CTXSYS', 'MDSYS');
How about the following 3 statements?
-- change to your schema
ALTER SESSION SET CURRENT_SCHEMA=yourSchemaName;
-- check current schema
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL;
-- generate drop table statements
SELECT 'drop table ', table_name, 'cascade constraints;' FROM ALL_TABLES WHERE OWNER = 'yourSchemaName';
COPY the RESULT and PASTE and RUN.

db append a certain value to a particular field for all records

I have a groups table which has a field called permissions that has a pipe separated value.
for example
permissions='1-1|2-2|3-2|4-2|5-2|6-2|7-1|8-2|9-2|10-2|11-2|12-2|13-3|14-2|15-2|16-2|
Now I want to append the following values to all permissions field in the groups table:
|17-0|18-0|19-0
So i need something like
UPDATE groups SET permissions='existing value+|17-0|18-0|19-0';
I am not sure how to go about it.( I am using informix)
Reading a bit about this, in informix concatenation with null gives null. If the field permissions can be null, the update needs to be changed a bit.
UPDATE groups SET permissions = NVL(permissions || '|17-0|18-0|19-0', '17-0|18-0|19-0');
If permissions can't be null, then this is enough:
UPDATE groups SET permissions = permissions || '|17-0|18-0|19-0';
UPDATE groups SET permissions = permissions + '|17-0|18-0|19-0'
Google says, use ConCat command or ||