ORACLE - concat 2 columns from join in another schema - oracle11g

I have a table where I store "id" values of employees. Now I need write a Select from that table with concatenation of Name & Surname of that employees. This select works ok when I do concatenation from same schema:
SELECT NAME || ' ' || SURNAME "Employee"
FROM Schema1.Table1
LEFT JOIN Schema1.Table2 u
ON Manager = u.ID
ORDER BY ID.Table1;
But when I do concatenation of same kind of data (same column types and names also) from different schema, I receive "invalid number" error in "u.ID":
SELECT NAME || ' ' || SURNAME "Employee"
FROM Schema1.Table1
LEFT JOIN Schema2.Table2 u
ON Manager = u.ID
ORDER BY ID.Table1;
Why isn't same Select working in both cases, and what should 2nd Select be like ?
Thanks for help in advance !

Sorry, my bad, second schema had a Varchar2 type of something I have Number type in my schema. To_Char solved my problem:
SELECT NAME || ' ' || SURNAME "Employee"
FROM Schema1.Table1
LEFT JOIN Schema2.Table2 u
ON to_char(Manager) = u.ID
ORDER BY ID.Table1;

Related

Ambiguity between column alias and another column's name

I have got some tables with overlapping column names and I want to combine those columns into one column with the same name as alias, e.g.
select a.name || " " || b.name as name from a join b
This works fine. However, if I want to order them, e.g.
order by lower(name) asc
I get the error ambiguous column name: name, because sqlite doesn't know whether to use a's, b's or the selection's name column. My question is whether it is possible to specifically chose the selection's name column (without giving it a different alias)? Maybe there is some nice <keyword> such that <keyword>.name results in the selection's name column. Or is there really no better solution than
with tmp as (select tmp_a.name || " " || tmp_b.name as name from tmp_a join tmp_b)
select name from tmp order by lower(name) asc
I don't think that the use of the CTE or a subquery is not a good solution.
If you don't like them, the only way to do it is to repeat the expression in the ORDER BY clause:
select a.name || ' ' || b.name as name
from a join b
on ......
order by lower(a.name || ' ' || b.name)

How can I bulk rename table columns in a SQLite database?

I created a whole bunch of SQLite database tables. Many columns in the tables have names with spaces, which I'm now realizing was not such a brilliant idea. Is there a way to write one command which will get rid of all spaces in all columns in all tables? I know I can do it one at a time (all potential duplicates seem to address this issue rather than my issue) but it's going to take me forever. Any ideas on how I can do this?
Use the following SQL to find all of the column names that contain spaces. I also included SQL to generate a new name.
SELECT t.name as tablename, c.name as badcol, replace(c.name, ' ','_') as newcolname
FROM sqlite_master t
JOIN pragma_table_info(t.name) c
WHERE t.type = 'table' AND c.name like '% %';
From here you would have to generate alter statements looking like this:
ALTER table <tablename> RENAME COLUMN <badcol> to <newcolname>;
While I cant figure how to directly pass the list of parms to the Alter table command you can use the following SQL to generate the alter commands for you then just copy/paste the result and execute the list of them.
SELECT ('ALTER TABLE ' || t.name || ' RENAME COLUMN ' || '[' || c.name || ']'
|| ' TO ' || '[' || REPLACE(c.name, ' ','_') || '];')
FROM sqlite_master t
JOIN pragma_table_info(t.name) c
WHERE t.type = 'table' AND c.name like '% %';
In this SQL I replaced the spaces in col names with underscores but you can see where you could replace the REPLACE function with the column renaming solution you desire.

The default schema does not exist. error when executing stored procedure

Create stored procedure
CREATE PROCEDURE dbo.GetBookList
-- Add the parameters for the stored procedure here
AS
BEGIN
create table BookAuthersName
(
BookId int,
Names varchar(255)
);
insert into BookAthersName(BookId,Names)
select
t1.BookId,
stuff((select ', '+a.Name
from BookAuthers t2 join Authers a on t2.AutherId= a.Id where t1.BookId = t2.BookId
for xml path('')),
1,2,'') [Values]
from dbo.BookAuthers t1
group by t1.BookId
create table BookSubjectNames
(
BookTypeId int,
Names varchar(255)
);
insert into BookSubjectNames(BookTypeId,Names)
select
t1.BookTypeId,
stuff((select ', '+a.Name
from BookTypeSubjects t2 join Subjects a on t2.SubjectId= a.Id where t1.BookTypeId = t2.BookTypeId
for xml path('')),
1,2,'') [Values]
from dbo.BookTypeSubjects t1
group by t1.BookTypeId
SELECT dbo.BooksType.Name, dbo.BooksType.BuyingDate AS [Buying Date], dbo.Publishers.Name AS [Publisher Name], dbo.Inventory.TotalBooks AS [Total Books],
dbo.Inventory.TotalIssuedBooks AS [Total Issued Books], ban.Names as [Auther Names] ,bsn.Names as [Subject Names]
FROM dbo.BooksType INNER JOIN dbo.Inventory
ON dbo.BooksType.Id = dbo.Inventory.BookTypeId
INNER JOIN dbo.Publishers ON dbo.BooksType.PublisherId = dbo.Publishers.Id
inner join BookAuthersName ban on dbo.BooksType.Id = ban .BookId
inner join BookSubjectNames bsn on dbo.BooksType.Id = bsn .BookTypeId
drop table BookAuthersName
drop table BookSubjectNames
END
It gives error when executing through a .net website. Error is
The default schema does not exist. error when executing stored
procedure.
Gone through some solutions but none seems to help
I am using Integrated Security=True in webconfig connection string
First thing you must get schema name with query
select schema_name()
If the schema name is null you must try set default name with query
ALTER USER [dbo.database_name] WITH DEFAULT_SCHEMA = [dbo];

SQL Server : Declare is causing problems in query

I currently have an issue with SQL Server which I can't figure out.
The error is:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
SQL:
DECLARE #IdUser INT
Select
#IdUser = Id,
Username,
(Select Count(*) From GagsLikes where Userid = #IdUser And Good = 1) as GagLikes,
(Select Count(*) From GagsViews where UserID = #IdUser),
(Select Count(*) From Gags Where UserID = #IdUser) as GagViews
From
Users
Order by
GagLikes, GagViews
Thanks in advance!
You may not use that variable as you do it try this:
Select
U.Id,
U.Username,
(Select Count(*) From GagsLikes where Userid = U.Id And Good = 1) as GagLikes,
(Select Count(*) From GagsViews where UserID = U.Id),
(Select Count(*) From Gags Where UserID = U.Id) as GagViews
From
Users AS U
Order by
GagLikes, GagViews
your table might return more then one rows . make sure that your table return only one row otherwise it can
t store multiple id's in to one int variable .

Efficient query for searching a single character from a huge table with 100 fields

I have a table that has 100 fields. The table has around 1000 records. I want to know whether any field of any record has a character "" in it. Owning to the large number of fields (100), I want to know how I can form a query to know whether any field in any record has a character "" in it. I am not supposed to be use dynamic SQL :(
OK, without using dynamic SQL you can build the static SQL you need like this:
select 'select pk, ''' || column_name
|| ''' col from mytable where '
|| column_name || ' like ''%x%'';'
from user_tab_columns
where table_name = 'MYTABLE';
That will output a select statement for each column, which you can then run as a SQL Plus script:
select pk, 'PK' col from mytable where PK like '%x%';
select pk, 'COL2' col from mytable where COL2 like '%x%';
select pk, 'COL3' col from mytable where COL3 like '%x%';

Resources