<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forum PostgreSQL - CREATE TABLE przez funkcje]]></title>
	<link rel="self" href="http://forum.postgresql.org.pl/extern.php?action=feed&amp;tid=146&amp;type=atom"/>
	<updated>2009-03-02T21:43:43Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.postgresql.org.pl/viewtopic.php?id=146</id>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=578#p578"/>
			<content type="html"><![CDATA[Zmieniłem na integer dział jak trzeba. WIELKI SZACUNEK DLA WIEDZY Z POSTGRESA 
[code]CREATE OR REPLACE FUNCTION f_sint_CreateTable_tbl(integer)
RETURNS void AS $$
DECLARE
    str varchar;
begin
        select ($1::integer)::varchar into str;
        execute ' CREATE TABLE "'||str||'"(
          dtDataGodzina timestamp(0) without time zone NOT NULL,
          idfkPojazd smallint NOT NULL,
          idfkAwarieSkroty smallint NOT NULL,
          sintNrBitu smallint NOT NULL,
          intWartosc integer
        ) WITH OIDS';
end;
$$ LANGUAGE plpgsql;[/code]]]></content>
			<author>
				<name><![CDATA[WitekS]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=760</uri>
			</author>
			<updated>2009-03-02T21:43:43Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=578#p578</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=577#p577"/>
			<content type="html"><![CDATA[Jak sa bledy, znaczy trza poprawić :). Może tak
[code]CREATE OR REPLACE FUNCTION f_sint_CreateTable_tbl(smallint)
RETURNS void AS $$
DECLARE
    str varchar;
begin
        select ($1::smallint)::varchar into str;
        execute ' CREATE TABLE "'||str||'"(
          dtDataGodzina timestamp(0) without time zone NOT NULL,
          idfkPojazd smallint NOT NULL,
          idfkAwarieSkroty smallint NOT NULL,
          sintNrBitu smallint NOT NULL,
          intWartosc integer
        ) WITH OIDS';
end;
$$ LANGUAGE plpgsql;[/code]
Jedna uwaga jesli chcesz używać smallint to przy wywołaniu funkcji musisz rzutować argument np
[code]select f_sint_CreateTable_tbl(112::smallint);[/code]
Jak tego nie zrobisz to przy wywolaniu
[code]select f_sint_CreateTable_tbl(112);[/code]
postgres bedzie szukal funkcji z argumentem integer i zgłosi bląd bo definicji takiej nie ma (może warto zmienić typ ze smallint na integer)?]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-03-02T20:29:47Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=577#p577</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=576#p576"/>
			<content type="html"><![CDATA[wykombinowałem coś takiego ale coś tu jest nie tak:
[code]CREATE OR REPLACE FUNCTION f_sint_CreateTable_tbl(smallint)
RETURNS void AS $$
DECLARE 
    str varchar;
begin
str:=($1::smallint)::varchar;
execute ' CREATE TABLE '||str||'(
          dtDataGodzina timestamp(0) without time zone NOT NULL,
          idfkPojazd smallint NOT NULL,
          idfkAwarieSkroty smallint NOT NULL,
          sintNrBitu smallint NOT NULL,
          intWartosc integer
) WITH OIDS';
end;
$$ LANGUAGE plpgsql;[/code]
testuję tak:

[code]SELECT f_sint_CreateTable_tbl(1234);[/code]
i wywołaniu wywala błędy]]></content>
			<author>
				<name><![CDATA[WitekS]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=760</uri>
			</author>
			<updated>2009-03-02T20:19:27Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=576#p576</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=575#p575"/>
			<content type="html"><![CDATA[Operator rzutowania w postgresie to :: np
[code]select (1::smallint)::varchar;[/code]
Ale postgres jest dosc wrazliwy jesli chodzi o nazewnictwo tabel i pewnie bedziesz musial dodac " " w tym execute, polecenie tworzenia takiej tabeli z poziomu psql powinno byc 
[code]create table "123" (...)[/code]
Cos takiegop
[code]create table 123 (..[/code]
raczej nie przejdzie]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-03-02T19:39:55Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=575#p575</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=574#p574"/>
			<content type="html"><![CDATA[No właśnie nazwą tabeli jest liczba którą zwraca mi funkcja. Tabela będzie wpisywana przez wyzwalacz jednak jeżeli takiej tabeli jeszcze nie będzie to muszę ją utworzyć. Nazwą tej tabeli będzie liczba typu smallint zwracana przez inną funkcję.]]></content>
			<author>
				<name><![CDATA[WitekS]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=760</uri>
			</author>
			<updated>2009-03-02T19:12:08Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=574#p574</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=573#p573"/>
			<content type="html"><![CDATA[a co niby ten smallint ma przechowywac, nazwe tabeli? Nazwa tabeli ma byc liczba?]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-03-02T18:30:01Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=573#p573</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=572#p572"/>
			<content type="html"><![CDATA[Wielkie dzięki jest tylko jeden drobiazg jako parametr czyli nazwę  tabeli do utworzenia mam liczbę typu smallint którą zwraca mi inna funkcja żeby zadziałało muszę zrobić konwersję ze smallint na varchar albo w funkcji która zwraca albo w tej tylko nie wiem jak to zrobić. Szukałem informacji na temat PL/pgSQL niestety nie wiele znalazłem przykładów i informacji do MySQL jest dużo więcej.]]></content>
			<author>
				<name><![CDATA[WitekS]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=760</uri>
			</author>
			<updated>2009-03-02T18:28:50Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=572#p572</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=571#p571"/>
			<content type="html"><![CDATA[Może użyj dynamicznego sqla np tak
[code]CREATE OR REPLACE FUNCTION f_sint_CreateTable_tbl(varchar)
RETURNS void AS $$
begin
execute ' CREATE TABLE '||$1||'(
          dtDataGodzina timestamp(0) without time zone NOT NULL,
          idfkPojazd smallint NOT NULL,
          idfkAwarieSkroty smallint NOT NULL,
          sintNrBitu smallint NOT NULL,
          intWartosc integer
) WITH OIDS';
end;
$$ LANGUAGE plpgsql;[/code]]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-03-02T17:05:19Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=571#p571</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[CREATE TABLE przez funkcje]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=570#p570"/>
			<content type="html"><![CDATA[Witam!

Potrzebuję napisać funkcję która utworzy tabelę o zdefiniowanej strukturze i nazwie podanej jako parametr smallint. Mam funkcję napisaną w języku SQL chciałbym zamiast NazwaZmiennej użyć parametru funkcji:

[code]CREATE OR REPLACE FUNCTION f_sint_CreateTable_tbl(smallint) 
RETURNS void AS $$

CREATE TABLE NazwaZmiennej
(
  "dtDataGodzina" timestamp(0) without time zone NOT NULL,
  "idfkPojazd" smallint NOT NULL,
  "idfkAwarieSkroty" smallint NOT NULL,
  "sintNrBitu" smallint NOT NULL,
  "intWartosc" integer
)
WITH (OIDS=TRUE);

$$ LANGUAGE SQL;[/code]]]></content>
			<author>
				<name><![CDATA[WitekS]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=760</uri>
			</author>
			<updated>2009-03-02T13:43:34Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=570#p570</id>
		</entry>
</feed>
