How to check a table exists in another schema - laravel

How to check if a table exists in a given schema using laravel.
eg: I want to check if a table exists in acc schema
I tried this but did not get.
$apty_tblnme = 'acc.accounts_appropriation_allotment_types';
$apty_exist = (!Schema::hasTable($apty_tblnme));//--if table not, get 1,

$apty_tblnme = DB::connection('pgsql')
->select("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = ? AND table_name = ?)",['acc','xxx']);
here,
acc-schema,
xxx- table name

Related

oracle stored procedure updating one table with the help of another table

TABLE A : name empid activeind
TABLE B : name empid activeind
update table A based on table B comparing empid from both the table using Oracle stored procedure
Here's one option.
create or replace procedure p_upd is
begin
merge into a
using b
on (b.empid = a.empid)
when matched then update set a.name = b.name,
a.activeind = b.activeind;
end;
/
[EDIT] Based on your comment: you used wrong syntax. UPDATE should be
UPDATE a
SET (a.name, a.activeind) =
(SELECT b.name, b.activeind
FROM b
WHERE b.empid = a.empid)
WHERE EXISTS
(SELECT NULL
FROM b
WHERE b.empid = a.empid);
Why exists? Without it, you'd set name and activeid values to null for all a.empid rows that don't exist in b table.

In sql without insert values in table, how i can see column of table

SQL> create table justlike(customerid varchar(19),first_name varchar(40),last_name varchar(100),Address varchar(50),city varchar(30),pincode varchar(10),state varchar(20));
Just use:
desc <table_name>;
This will print the description of your table columns
in your case:
desc justlike;
You can always check the table definition, in case you are using Oracle, by running below query -
SELECT * FROM USER_TAB_COLS
WHERE TABLE_NAME = 'JUSTLIKE';
OR you can write a select on table itself -
SELECT * FROM JUSTLIKE;

How to check if table has specific grants before inserting a record in a table?

Scenario: I have a config table which includes meta-data about log tables (e.g. Table_name, table_owner). I want to add a trigger to this config table which essentially checks if the table record that is to be inserted, has the specific grant (delete) if not, then don't allow that to happen. How can I go about this?
Another way to do it is to forget about triggers but use a VIEW...WITH CHECK OPTION to control that no one inserts data they shouldn't.
Suppose your config table looks like this:
CREATE TABLE my_config
( owner VARCHAR2(30) NOT NULL,
table_name VARCHAR2(30) NOT NULL,
other_stuff VARCHAR2(500),
CONSTRAINT my_config_pk PRIMARY KEY ( owner, table_name ) );
Create a view to handle inserts
The conditions of the view limit it to only include tables on which the current user has DELETE privileges.
The WITH CHECK OPTION will ensure than no one may use the view to insert or update data that does not satisfy the view.
CREATE VIEW my_config_ins_v
AS
SELECT * FROM my_config c
WHERE EXISTS ( SELECT 'user has delete privs'
FROM user_tab_privs p
WHERE p.owner = c.owner
AND p.grantee = user
AND p.table_name = c.table_name
AND p.privilege = 'DELETE' )
WITH CHECK OPTION;
Try it out
INSERT INTO my_config_ins_v VALUES ('USER_1','TABLE_I_HAVE_ACCESS_TO', 'STUFF');
-- 1 row inserted.
INSERT INTO my_config_ins_v VALUES ('SYS','OBJ$', 'STUFF');
-- ORA-01402: view WITH CHECK OPTION where-clause violation
Naturally, for this to be effective, you cannot
GRANT INSERT ON my_config TO anyone; -- don't do this
Instead:
GRANT INSERT ON my_config_ins_v TO anyone;

MERGE table from two different schema

I have two same tables from two different schema, db link between schema already created. The table in schema B (old) need to be merged to schema A(new). The table structure between 2 schemas are identical, except an additional timestamp column exist in schema A.The timestamp value is defaulted as SYSDATE when merge.
When i tried to run the following script in schema B, it complains with
ORA-00904: "A"."LAST_UPDATED_TIMESTAMP": invalid identifier
MERGE INTO LOOKUP_RANGE A
USING (SELECT RANGE_NAME,RANGE_TYPE,RANGE_MIN,RANGE_MAX,RANGE_DESC FROM LOOKUP_RANGE) B
ON (A.RANGE_NAME = B.RANGE_NAME AND A.RANGE_TYPE = B.RANGE_TYPE)
WHEN MATCHED THEN UPDATE SET A.RANGE_MIN = B.RANGE_MIN, A.RANGE_MAX = B.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP = SYSDATE,A.RANGE_DESC = B.RANGE_DESC
WHEN NOT MATCHED THEN INSERT (A.RANGE_MIN,A.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP,A.RANGE_DESC) VALUES (A.RANGE_MIN,A.RANGE_MAX,SYSDATE,A.RANGE_DESC);
The key column in ON is the primary key of table.
Am I execute in wrong schema? It should run in schema A?
The below code work perfectly. B_MIG_61_TO_74 is the database link created, OLD is the schema name of old schema, NEW is the schema name of new schema
MERGE INTO NEW.LOOKUP_RANGE A
USING (SELECT RANGE_NAME,RANGE_TYPE,RANGE_MIN,RANGE_MAX,RANGE_DESC FROM OLD.LOOKUP_RANGE#DB_MIG_61_TO_74) B
ON (A.RANGE_NAME = B.RANGE_NAME AND A.RANGE_TYPE = B.RANGE_TYPE)
WHEN MATCHED THEN UPDATE SET A.RANGE_MIN = B.RANGE_MIN, A.RANGE_MAX = B.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP = SYSDATE,A.RANGE_DESC = B.RANGE_DESC
WHEN NOT MATCHED THEN INSERT (A.RANGE_NAME,A.RANGE_TYPE,A.RANGE_MIN,A.RANGE_MAX,A.LAST_UPDATED_TIMESTAMP,A.RANGE_DESC) VALUES (B.RANGE_NAME,B.RANGE_TYPE,B.RANGE_MIN,B.RANGE_MAX,SYSDATE,B.RANGE_DESC);

How do I fetch column names from a db table into an array, using PostgreSQL and Ruby?

How can I iterate through all of the column headers in a table for a given database and add each column name individually to an array?
I know I can gather all of the information from the table using SELECT * FROM my_table but I need to work with just the column headers at this point. What is the best way to accomplish this?
I recommend using
select column_name from information_schema.columns where table_name='my_table'
as
SELECT * FROM my_table LIMIT 1
will not return column names if the table is empty.
http://www.postgresql.org/message-id/AANLkTilsjTAXyN5DghR3M2U4c8w48UVxhov4-8igMpd1#mail.gmail.com
Are you using the pg gem? If so, you can try this:
require 'pg'
# `db_config` must contain your database configuration
conn = PG::Connection.open(db_config)
column_names = conn.query("SELECT * FROM my_table LIMIT 1").first.keys

Resources