I have a flat file table schema (tab delimited) from another type of database (I don't know what type), but it gives me the basics of what I need such as column name, data type, description. This table has many columns.
What is the best way to create a table in SQL Server 2008 from this flat file?
Ex:
You can import file into a table with following columns
TableA:
TABLE_NAME, COLUMN_NAME, DATA_TYPE, DESCRIPTION
Then generate create statement using script below and copy output and save as sql script
SELECT 'CREATE TABLE TABLE1 (' + cols + ')' FROM (
SELECT SUBSTRING(
(SELECT ',' + s.COLUMN_NAME + ' ' DATA_TYPE
FROM tableA s WHERE TABLE_NAME = 'TABLE1'
FOR XML PATH('')),2,200000) AS Cols) A
Related
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.
I have 4 locations Chennai, Banglore, Hyderabad, Mumbai.
Depending on user selection from dropdown, I need to gennerate an id like this:
If they select Chennai - CHE001,CH002,CHE002,CHE003,CHE004.....etc
If they select Mumbai - MUM001,MUM002,MUM003,MUM004.......etc
If they select Hyderabad - HYD001,HYD002,HYD003,HYD004........etc
like that in database it has to save
Like that auto generated id as has to save in database but it must be in unique.... I don't how it will work with SQL functions are stored procedure, asp.net .. please kindly help for this issue.
Declare #Selected varchar(100)='Chennai'
Select Stuff((Select Distinct ',' +ID
From (Select Top 999 ID=Upper(Left(#Selected,3))+Format(Row_Number() Over (Order By (Select null)),'000') From master..spt_values ) A
For XML Path ('')),1,1,'')
Returns
CHE001,CHE002,CHE003,CHE004,CHE005,CHE006,CHE007,CHE008,CHE009,CHE010,{...},CHE997,CHE998,CHE999
Or if you need rows
Declare #Selected varchar(100)='Chennai'
Select Top 999 ID=Upper(Left(#Selected,3))+Format(Row_Number() Over (Order By (Select null)),'000') From master..spt_values
Returns
ID
CHE001
CHE002
CHE003
CHE004
CHE005
{...}
CHE997
CHE998
CHE999
You can create stored procedure similar to below:
BEGIN
DECLARE #CITY VARCHAR(MAX) = 'MUMBAI'
DECLARE #CITYCODE VARCHAR(3) = SUBSTRING(#CITY,1,3)
INSERT INTO TBL (ID)
SELECT #CITYCODE + RIGHT('000' + CAST((CAST(REPLACE(ISNULL(MAX(ID),#CITYCODE + '000'),#CITYCODE,'') AS INT) + 1) AS VARCHAR),3)
FROM TBL
WHERE ID LIKE #CITYCODE + '%'
END
I want to use ISNULL with all columns of a table.
Like this
SELECT ISNULL(* , 'NA') FROM #tbl
I know its a wrong way I have to specify each column name separately to use ISNULL.
But in my case #tbl is a temporary table and columns are created dynamically
like this:
EXEC ('ALTER TABLE #tbl ADD [' + #bno + '] varchar(30)')
Due to this I can't use column names in select query because column names are always different.
So what should I do?
Change your dynamic SQL to:
EXEC ('ALTER TABLE #tbl ADD [' + #bno + '] varchar(30) NULL DEFAULT(''NA'')')
Try to get fields of your temprary table from the tempdb.sys.columns table then iterate over these:
select c.*
from tempdb.sys.columns c
inner join tempdb.sys.tables t ON c.object_id = t.object_id
where t.name like '#TableName%'
I am running the following query on an sqlite3 database:
SELECT file.id AS fileID, file.path
FROM file
JOIN (SELECT tag_file_map.fileID,tag.tagname
FROM tag_file_map, tag JOIN tag ON tag_file_map.tagID = tag.id)
ON tag_file_map.fileID = file.id
WHERE tag.tagname = 'tag1' AND tag.tagname= 'tag2';
It gives me the following error: "ambiguous column name: tag.tagname"
Google seems to say that this error is produced when one or more tables share a column name and the specific table of the column is not specified. However, here, the table name IS specified. Plus, there is no other column with the name "tagname" in the entire database, so it shouldn't be ambiguous with or without the table name. Is this an sqlite problem, or is something off with my syntax?
on you inner select, change
FROM tag_file_map, tag JOIN tag on tag_file_map....
to
FROM tag_file_map JOIN tag on tag_file_map....
I think this will work...
SELECT file.id AS fileID, file.path
FROM file
JOIN
(SELECT tag_file_map.fileID, tag.tagname FROM tag_file_map
JOIN tag ON tag_file_map.tagID = tag.id
WHERE tag.tagname = 'tag1' AND tag.tagname= 'tag2')
ON tag_file_map.fileID = file.id;
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%';